如何在 C# 中反序列化包含多种类型元素的 XML 数组 [英] How to deserialize an XML array containing multiple types of elements in C#

查看:25
本文介绍了如何在 C# 中反序列化包含多种类型元素的 XML 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化以下文件:

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>

使用以下代码:

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	est.xml", FileMode.Open, FileAccess.Read))
            {
                var ser = new XmlSerializer(typeof(League));
                League league = (League)ser.Deserialize(reader);
            }
        }
    }
}

我希望得到一个 League 对象,其中包含一个包含 3 个元素的 Skaters 列表和一个包含 2 个元素的 Goalies 列表.我确实得到了预期的溜冰者名单,但守门员名单是空的.我做错了什么?

I'm expecting to get back a League object containing a Skaters list with 3 elements and a Goalies list with 2 elements. I do get the expected Skaters list but the Goalies list is empty. 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; }

将两种元素类型映射到单个集合中.最坏的情况,SomeCommonBaseClass 可能是 object:

which maps the two element types inside a single collection. Worst case, SomeCommonBaseClass could be 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:

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

这篇关于如何在 C# 中反序列化包含多种类型元素的 XML 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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