将XML反序列化为包含C#中对象列表的对象

时间:2017-07-11 06:16:23

标签: c# xml xmlserializer xml-deserialization

我有跟随我要反序列化为对象的xml。

<result>
    <reporttype>2</reporttype>
    <items>
        <item>
            <sku>0B0005</sku>
            <style>0B0005.DAK.GREY</style>
            <reason>Barcode cannot be moved to different SKUs</reason>
        </item>
        <item>
            <sku>0B0006</sku>
            <style>0B0006.DAK.GREY</style>
            <reason>Barcode cannot be moved to different SKUs</reason>
        </item>
    </items>
</result>

但是下面的代码没有填充项目列表,有人可以指出我在这里做错了吗

string inputString = @"<result><reporttype>2</reporttype><items><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item></items></result>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(inputString);

XmlSerializer serializer = new XmlSerializer(typeof(Result));
StringReader rdr = new StringReader(doc.InnerXml);
Result resultingMessage = (Result)serializer.Deserialize(rdr);

public enum ReportType {
    [XmlEnum("0")]
    InternalErrorReport,
    [XmlEnum("1")]
    ErrorReport,
    [XmlEnum("2")]
    InternalSuccessReport
}

[XmlRoot(ElementName = "result")]
public class Result {
    [XmlElement(ElementName = "reporttype")]
    public ReportType reportType { get; set; }
    [XmlElement(ElementName = "items")]
    public List<Item> items = new List<Item>();

    public string error { get; set; }

    public class Item {
        [XmlElement(ElementName = "sku")]
        string sku { get; set; }
        [XmlElement(ElementName = "style")]
        string style { get; set; }
        [XmlElement(ElementName = "reason")]
        string reason { get; set; }
    }
}

或者有更好的方法吗?

4 个答案:

答案 0 :(得分:1)

变量需要公开。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(Result));
            StringReader rdr = new StringReader(xml);
            Result resultingMessage = (Result)serializer.Deserialize(rdr);


        }
    }
    public enum ReportType
    {
        [XmlEnum("0")]
        InternalErrorReport,
        [XmlEnum("1")]
        ErrorReport,
        [XmlEnum("2")]
        InternalSuccessReport
    }

    [XmlRoot(ElementName = "result")]
    public class Result
    {
        [XmlElement(ElementName = "reporttype")]
        public ReportType reportType { get; set; }
        public Items items { get; set; }
        public string error { get; set; }


    }
    [XmlRoot("items")]
    public class Items
    {
        [XmlElement(ElementName = "item")]
        public List<Item> items = new List<Item>();
    }
    [XmlRoot("item")]
    public class Item
    {
        [XmlElement(ElementName = "sku")]
        public string sku { get; set; }
        [XmlElement(ElementName = "style")]
        public string style { get; set; }
        [XmlElement(ElementName = "reason")]
        public string reason { get; set; }
    }
}

答案 1 :(得分:1)

如此处所述,您需要将列表标记为XmlArray,同时指定XmlArrayItem:Deserializing nested lists with XmlSerializer

所以代码变成:

        string inputString = @"<result><error>error test</error><reporttype>2</reporttype><items><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item></items></result>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(inputString);

        XmlSerializer serializer = new XmlSerializer(typeof(Result));
        object resultingMessage = null;
        using (StringReader rdr = new StringReader(doc.InnerXml)) {
            resultingMessage = (Result)serializer.Deserialize(rdr);
        }

和班级:

public enum ReportType {
    [XmlEnum("0")]
    InternalErrorReport,
    [XmlEnum("1")]
    ErrorReport,
    [XmlEnum("2")]
    InternalSuccessReport
}

[XmlRoot(ElementName = "result")]
public class Result {
    [XmlElement(ElementName = "reporttype")]
    public ReportType reporttype { get; set; }
    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> items { get; set; }
    [XmlElement(ElementName = "error")]
    public string error { get; set; }

    [XmlRoot(ElementName = "items\\item")]
    public class Item {
        [XmlElement(ElementName = "sku")]
        public string sku { get; set; }
        [XmlElement(ElementName = "style")]
        public string style { get; set; }
        [XmlElement(ElementName = "reason")]
        public string reason { get; set; }
    }

}

请注意,我将字符串阅读器封装在一个使用块中,以便在读取结束后处理该对象。

答案 2 :(得分:0)

项应该是Xmlroot元素并且它包含XmlElement项,您必须告诉它何时反序列化为对象。试试这个:

public class Item
{
    [XmlElement(ElementName = "sku")]
    public string Sku { get; set; }
    [XmlElement(ElementName = "style")]
    public string Style { get; set; }
    [XmlElement(ElementName = "reason")]
    public string Reason { get; set; }
}

[XmlRoot(ElementName = "items")]
public class Items
{
    [XmlElement(ElementName = "item")]
    public List<Item> Item { get; set; }
}

[XmlRoot(ElementName = "result")]
public class Result
{
    [XmlElement(ElementName = "reporttype")]
    public string Reporttype { get; set; }
    [XmlElement(ElementName = "items")]
    public Items Items { get; set; }
}

答案 3 :(得分:0)

您可以为items属性添加两个属性 - 以满足序列化

[XmlRoot(ElementName = "result")]
public class Result 
{
    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> items = new List<Item>();
}

或者只为Item类设置type属性(XmlType) 然后就足以仅对XmlArray属性使用Result.items属性。或者根本不使用任何属性,因为属性的名称与xml中元素的名称匹配。

[XmlType("item")]
public class Item 
{
    [XmlElement(ElementName = "sku")]
    public string sku { get; set; }
    [XmlElement(ElementName = "style")]
    public string style { get; set; }
    [XmlElement(ElementName = "reason")]
    public string reason { get; set; }
}

当然公开属性