简体   繁体   English

如何在C#中反序列化包含多种类型元素的XML数组

[英]How to deserialize an XML array containing multiple types of elements in C#

I'm trying to deserialize the following file: 我正在尝试反序列化以下文件:

<league>
    <players>
        <skater>
            <name>Wayne Stamkos</name>
            <goals>23</goals>
            <assists>34</assists>
        </skater>
        <skater>
            <name>Sidney Lindros</name>
            <goals>41</goals>
            <assists>44</assists>
        </skater>
        <goalie>
            <name>Martin Roy</name>
            <wins>15</wins>
            <losses>12</losses>
        </goalie>
        <skater>
            <name>Paul Forsberg</name>
            <goals>21</goals>
            <assists>51</assists>
        </skater>
        <goalie>
            <name>Roberto Rinne</name>
            <wins>18</wins>
            <losses>23</losses>
        </goalie>
    </players>
</league>

With the following code: 使用以下代码:

namespace ConsoleApplication2
{
    [XmlRoot("league")]
    public class League
    {
        [XmlArray("players")]
        [XmlArrayItem("skater")]
        public List<Skater> skaters { get; set; }
        [XmlArrayItem("goalie")]
        public List<Goalie> goalies { get; set; }
    }

    public class Skater
    {
        [XmlElement("name")]
        public string Name;
        [XmlElement("goals")]
        public int Goals;
        [XmlElement("assists")]
        public int Assists;
    }

    public class Goalie
    {
        [XmlElement("name")]
        public string Name;
        [XmlElement("wins")]
        public int Wins;
        [XmlElement("losses")]
        public int Losses;
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream reader = new FileStream(@"C:\Temp\test.xml", FileMode.Open, FileAccess.Read))
            {
                var ser = new XmlSerializer(typeof(League));
                League league = (League)ser.Deserialize(reader);
            }
        }
    }
}

I'm expecting to get back a League object containing a Skaters list with 3 elements and a Goalies list with 2 elements. 我期待回到一个联盟对象,其中包含一个包含3个元素的Skaters列表和一个包含2个元素的守门员列表。 I do get the expected Skaters list but the Goalies list is empty. 我确实得到了预期的Skaters列表但是守门员列表是空的。 What am I doing wrong? 我究竟做错了什么?

There are two ways to do this; 有两种方法可以做到这一点; the first is to do something like: 首先是做一些事情:

[XmlArray("players")]
[XmlArrayItem("skater", Type=typeof(Skater))]
[XmlArrayItem("goalie", Type=typeof(Goalie))]
public List<SomeCommonBaseClass> Players { get; set; }

which maps the two element types inside a single collection. 它将两个元素类型映射到单个集合中。 Worst case, SomeCommonBaseClass could be object : 最糟糕的情况是, SomeCommonBaseClass可能是object

[XmlArray("players")]
[XmlArrayItem("skater", Type=typeof(Skater))]
[XmlArrayItem("goalie", Type=typeof(Goalie))]
public List<object> Players { get; set; }

The second is to make <players> map to a wrapper object: 第二个是使<players>映射到包装器对象:

[XmlElement("players")]
public Players Players { get;set;}
...
public class Players
{
    [XmlElement("skater")]
    public List<Skater> Skaters {get;set;}

    [XmlElement("goalie")]
    public List<Goalie> Goalies {get;set;}
}

Which to choose depends on the circumstance; 选择哪个取决于具体情况; the latter allows things like "at most one goalie", by changing it to: 后者允许“至多一个守门员”之类的东西,通过将其改为:

    [XmlElement("goalie")]
    public Goalie Goalie {get;set;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM