C# Xml 序列化 List<T> 具有 Xml 属性的后代

标签 c# list attributes generics xmlserializer

早上好,

我有一个继承自 List 的集合,它有一个公共(public)属性。 Xml 序列化程序不获取我的属性。列表项序列化良好。我已尝试 XmlAttribute 属性无济于事。你们有解决办法吗?

    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 = "\r\n", 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 解决了这个问题。如果存在更简单的解决方案,请发布!

    [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 序列化 List<T> 具有 Xml 属性的后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3416426/

相关文章:

C# 属性引用类型?

c# - 拖动时突出显示矩形区域

python - 按行拆分数据框并在 python 中生成数据框列表

c# - 为什么属性的属性必须是可读的?

c# - 在 C# 中实现多于一种类型对象的堆栈的最佳方法是什么?

c# - 用于在数字模式和冒号或换行符之间提取字符串的正则表达式

c# - 从类属性获取绑定(bind)值,默认绑定(bind)属性对我不起作用!

Python 双下划线修改

Python:如何在行和列的值之间创建对应矩阵?

c# - 如何将 ienumerable 集合附加到 C# 中的现有列表