How to deserialize xml using XmlArrayItem

做~自己de王妃 提交于 2020-01-02 19:12:33

问题


I want to deserialize xml like this:

<Product>
    <Classification>
        <NewClassification>1</NewClassification>
        <NewClassification>2</NewClassification>
        <NewClassification>3</NewClassification>
        <OldClassification>A</OldClassification>
        <OldClassification>B</OldClassification>
    </Classification>
</Product>

My classes are:

public class Product
{
    public Classification Classification { get; set; }
}

public class Classification
{
    [XmlArrayItem(typeof(int), ElementName = "NewClassification")]
    public List<int> NewClassificationList { get; set; }
    [XmlArrayItem(typeof(int), ElementName = "OldClassification")]
    public List<int> OldClassificationList { get; set; }
}

Why this code is wrong?


回答1:


Apart from the "int literal" issue commented on by "Bears will eat you" above, you can't serialize your class in the way you want with what you've got (at least I can't think of a way to do it) without resorting to custom serialisation (e.g. implementing IXmlSerializable or some other method).

Your basic problem is that you have two seperate collections in Classification, which you want to appear as a single collection.

The IXmlSerialization route is pretty simple though. Just implement it as follows on your Classification class, and you'll get the desired result.

public class Classification : IXmlSerializable
{
    public List<int> NewClassificationList { get; set; }
    public List<string> OldClassificationList { get; set; }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        foreach (var item in NewClassificationList)
        {
            writer.WriteElementString("NewClassification", item.ToString());
        }
        foreach (var item in OldClassificationList)
        {
            writer.WriteElementString("OldClassification", item.ToString());
        }
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }
}

Added ReadXml implementation

ReadXml can be implemented as follows:

public void ReadXml(System.Xml.XmlReader reader)
{
    var product = new Product();

    product.Classification = new Classification { NewClassificationList = new List<int>(),  ldClassificationList = new List<string>()};

    reader.ReadStartElement("Classification");

    while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
    {
        switch (reader.Name)
        {
            case "NewClassification":
                product.Classification.NewClassificationList.Add(reader.ReadElementContentAsInt());
                break;
            case "OldClassification":
                product.Classification.OldClassificationList.Add(reader.ReadElementContentAsString());
                break;
            default:
                throw new NotSupportedException("Unsupported node: " + reader.Name);
           } 
        }
        reader.ReadEndElement();
    }


来源:https://stackoverflow.com/questions/1874903/how-to-deserialize-xml-using-xmlarrayitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!