<small id='1PLIg'></small><noframes id='1PLIg'>

    1. <i id='1PLIg'><tr id='1PLIg'><dt id='1PLIg'><q id='1PLIg'><span id='1PLIg'><b id='1PLIg'><form id='1PLIg'><ins id='1PLIg'></ins><ul id='1PLIg'></ul><sub id='1PLIg'></sub></form><legend id='1PLIg'></legend><bdo id='1PLIg'><pre id='1PLIg'><center id='1PLIg'></center></pre></bdo></b><th id='1PLIg'></th></span></q></dt></tr></i><div id='1PLIg'><tfoot id='1PLIg'></tfoot><dl id='1PLIg'><fieldset id='1PLIg'></fieldset></dl></div>

        • <bdo id='1PLIg'></bdo><ul id='1PLIg'></ul>
        <tfoot id='1PLIg'></tfoot>
        <legend id='1PLIg'><style id='1PLIg'><dir id='1PLIg'><q id='1PLIg'></q></dir></style></legend>

        C# Xml 序列化列表&lt;T&gt;具有 Xml 属性的后代

        C# Xml Serializing Listlt;Tgt; descendant with Xml Attribute(C# Xml 序列化列表lt;Tgt;具有 Xml 属性的后代)

          <tfoot id='Oh0PO'></tfoot>
            <tbody id='Oh0PO'></tbody>

            <small id='Oh0PO'></small><noframes id='Oh0PO'>

            • <legend id='Oh0PO'><style id='Oh0PO'><dir id='Oh0PO'><q id='Oh0PO'></q></dir></style></legend>
              • <bdo id='Oh0PO'></bdo><ul id='Oh0PO'></ul>

                  <i id='Oh0PO'><tr id='Oh0PO'><dt id='Oh0PO'><q id='Oh0PO'><span id='Oh0PO'><b id='Oh0PO'><form id='Oh0PO'><ins id='Oh0PO'></ins><ul id='Oh0PO'></ul><sub id='Oh0PO'></sub></form><legend id='Oh0PO'></legend><bdo id='Oh0PO'><pre id='Oh0PO'><center id='Oh0PO'></center></pre></bdo></b><th id='Oh0PO'></th></span></q></dt></tr></i><div id='Oh0PO'><tfoot id='Oh0PO'></tfoot><dl id='Oh0PO'><fieldset id='Oh0PO'></fieldset></dl></div>
                  本文介绍了C# Xml 序列化列表&lt;T&gt;具有 Xml 属性的后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  早安,

                  我有一个来自 List 的集合,并且有一个公共属性.Xml 序列化程序不接受我的财产.列表项可以很好地序列化.我试过 XmlAttribute 属性无济于事.各位有解决办法吗?

                  I have a collection that descends from List and has a public property. The Xml serializer does not pick up my proeprty. The list items serialize fine. I have tried the XmlAttribute attribute to no avail. Do you guys have a solution?

                      public partial class MainWindow : Window
                  {
                      public MainWindow()
                      {
                          InitializeComponent();
                      }
                      private void button1_Click(object sender, RoutedEventArgs e)
                      {
                          var people = new PersonCollection
                          {
                              new Person { FirstName="Sue", Age=17 },
                              new Person { FirstName="Joe", Age=21 }
                          };
                          people.FavoritePerson = "Sue";
                  
                          var x = new XmlSerializer(people.GetType());
                          var b = new StringBuilder();
                          var w = XmlTextWriter.Create(b, new XmlWriterSettings { NewLineChars = "
                  ", Indent = true });
                          x.Serialize(w, people);
                          var s = b.ToString();
                      }
                  }
                  
                  [XmlRoot(ElementName="People")]
                  public class PersonCollection : List<Person>
                  {
                      //DOES NOT WORK! ARGHHH
                      [XmlAttribute]
                      public string FavoritePerson { get; set; }    
                  }
                  
                  public class Person
                  {
                      [XmlAttribute]
                      public string FirstName { get; set; }
                      [XmlAttribute]
                      public int Age { get; set; }
                  }
                  

                  我得到以下 xml

                  <?xml version="1.0" encoding="utf-16"?>
                          <People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                            <Person FirstName="Sue" Age="17" />
                            <Person FirstName="Joe" Age="21" />
                          </People>
                  

                  我想要这个

                  <?xml version="1.0" encoding="utf-16"?>
                          <People FavoritePerson="Sue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                            <Person FirstName="Sue" Age="17" />
                            <Person FirstName="Joe" Age="21" />
                          </People>
                  

                  推荐答案

                  我继续通过实现 IXmlSerializable 解决了这个问题.如果存在更简单的解决方案,请发布!

                  I went ahead and solved the problem by implementing IXmlSerializable. If a simpler solution exists, post it!

                      [XmlRoot(ElementName="People")]
                  public class PersonCollection : List<Person>, IXmlSerializable
                  {
                      //IT WORKS NOW!!! Too bad we have to implement IXmlSerializable
                      [XmlAttribute]
                      public string FavoritePerson { get; set; }
                  
                      public System.Xml.Schema.XmlSchema GetSchema()
                      {
                          return null;
                      }
                      public void ReadXml(XmlReader reader)
                      {
                          FavoritePerson = reader[0];            
                          while (reader.Read())
                          {
                              if (reader.Name == "Person")
                              {
                                  var p = new Person();
                                  p.FirstName = reader[0];
                                  p.Age = int.Parse( reader[1] ); 
                                  Add(p);
                              }
                          }
                      }
                      public void WriteXml(XmlWriter writer)
                      {
                          writer.WriteAttributeString("FavoritePerson", FavoritePerson);
                          foreach (var p in this)
                          {
                              writer.WriteStartElement("Person");
                              writer.WriteAttributeString("FirstName", p.FirstName);
                              writer.WriteAttributeString("Age", p.Age.ToString());
                              writer.WriteEndElement();            
                          }
                      }
                  }
                  

                  这篇关于C# Xml 序列化列表&lt;T&gt;具有 Xml 属性的后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                  <i id='ielHV'><tr id='ielHV'><dt id='ielHV'><q id='ielHV'><span id='ielHV'><b id='ielHV'><form id='ielHV'><ins id='ielHV'></ins><ul id='ielHV'></ul><sub id='ielHV'></sub></form><legend id='ielHV'></legend><bdo id='ielHV'><pre id='ielHV'><center id='ielHV'></center></pre></bdo></b><th id='ielHV'></th></span></q></dt></tr></i><div id='ielHV'><tfoot id='ielHV'></tfoot><dl id='ielHV'><fieldset id='ielHV'></fieldset></dl></div>
                      <tbody id='ielHV'></tbody>

                    <tfoot id='ielHV'></tfoot>
                    • <bdo id='ielHV'></bdo><ul id='ielHV'></ul>

                        <small id='ielHV'></small><noframes id='ielHV'>

                        <legend id='ielHV'><style id='ielHV'><dir id='ielHV'><q id='ielHV'></q></dir></style></legend>