如何使用XmlArrayItem反序列化XML [英] How to deserialize xml using XmlArrayItem

查看:571
本文介绍了如何使用XmlArrayItem反序列化XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要反序列化XML是这样的:

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>

我的类是:

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; }
}

为什么这个code是错的?

Why this code is wrong?

推荐答案

除了由评论对诠释文字问题熊会吃了你上面,你不能序列化类你想要什么的方式你有(至少我不能想办法做到这一点)
而不诉诸自定义序列(如实施的IXmlSerializable或其他方法)。

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.

该IXmlSerialization路线是pretty简单不过。只实现它在你的类分类如下,你会得到期望的结果。

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();
    }
}

新增的ReadXml实施

Added ReadXml implementation

的ReadXml可以实现如下:

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();
    }

这篇关于如何使用XmlArrayItem反序列化XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆