C# Xml Serializing Listlt;Tgt; descendant with Xml Attribute(C# Xml 序列化列表lt;Tgt;具有 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 序列化列表<T>具有 Xml 属性的后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# Xml 序列化列表<T>具有 Xml 属性的后代
				
        
 
            
        - 使用 rss + c# 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 
						
						
						
						
						