繁体   English   中英

如何反序列化 XML 数组项?

[英]How to Deserialize XML Array items?

我在反序列化 XML 时遇到问题。 我让 AccountInformation 工作,但它不适用于 Leauge 元素。 XML 不包含任何“Leauges”标签,我不想添加该标签以使其正常工作。 有没有其他方法可以“修复”它? 我尝试了不同的解决方案,但 leauges 的反序列化结果返回空。 我错过了什么?

任何帮助表示赞赏!

下面是我的代码:

更新:

我已经修改了代码和 XML,但无论如何我都不会工作。 我在这里缺少什么?

[Serializable]
[XmlRoot(ElementName = "LeaugeCollection", Namespace = "")]
public class LeagueCollection
{
    [XmlArray("Leagues")]
    [XmlArrayItem("League",typeof(League))]
    public  League[] League { get; set; }

    [XmlElement(ElementName = "AccountInformation")]
    public string AccountInformation { get; set; }

}

[Serializable()]
public class League
{
    [XmlElement(ElementName = "Id")]
    public int Id { get; set; }

    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "Country")]
    public string Country { get; set; }

    [XmlElement(ElementName = "Historical_Data")]
    public string Historical_Data { get; set; }

    [XmlElement(ElementName = "Fixtures")]
    public string Fixtures { get; set; }

    [XmlElement(ElementName = "LiveScore")]
    public string Livescore { get; set; }

    [XmlElement(ElementName = "NumberOfMatches")]
    public int NumberOfMatches { get; set; }

    [XmlElement(ElementName = "LatestMatch")]
    public DateTime LatestMatch { get; set; }
}

反序列化代码:

public static void Main(string[] args)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(LeagueCollection));

        TextReader reader = new StreamReader(@"C:\XmlFiles\XmlSoccer.xml");
        object obj = deserializer.Deserialize(reader);
        LeagueCollection XmlData = (LeagueCollection)obj;
        reader.Close();
    }

链接到 XML:

提前致谢!

您在图像中拥有的 XML 缺少实际的数组元素 (Leauges),它只有数组项元素 (Leauge),这就是您无法对其进行反序列化的原因!

更新:好的,尝试重现您的代码,我现在看到在您的 XML 中,您的元素拼写为“League”,而在您的代码“Leauge”中

先解决这个问题!

UPDATE2:根据我的评论进行编辑后,它似乎工作正常!

您缺少命名空间。 我不喜欢联赛和联赛。 联赛是不必要的。

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)
        {
            LeagueCollection leagueCollection = new LeagueCollection() {
                leagues = new Leagues() {
                    League = new List<League>() {
                       new League() {
                          Id = 1,
                          Name = "English Premier League",
                          Country = "England",
                          Historical_Data = "Yes",
                          Fixtures = "Yes",
                          Livescore = "Yes",
                          NumberOfMatches = 5700,
                          LatestMatch = DateTime.Parse( "2015-05-24T16:00:00+00:00")
                    },
                        new League() {
                            Id = 2,
                            Name = "English League Championship",
                            Country = "England",
                            Historical_Data = "Yes",
                            Fixtures = "Yes",
                            Livescore = "Yes",
                            NumberOfMatches = 5700,
                            LatestMatch = DateTime.Parse("2015-05-24T16:00:00+00:00")
                        }
                    }

                },
                AccountInformation = "Confidential info"
            };


            XmlSerializer serializer = new XmlSerializer(typeof(LeagueCollection));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, leagueCollection);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer deserializer = new XmlSerializer(typeof(LeagueCollection));

            XmlTextReader reader = new XmlTextReader(FILENAME);
            LeagueCollection XmlData = (LeagueCollection)deserializer.Deserialize(reader);
            reader.Close();

        }
    }
    [XmlRoot(ElementName = "LeaugeCollection")]
    public class LeagueCollection
    {
        [XmlElement("Leagues")]
        public Leagues leagues { get; set; }

        [XmlElement(ElementName = "AccountInformation")]
        public string AccountInformation { get; set; }

    }
    [XmlRoot("Leagues")]
    public class Leagues
    {
        [XmlElement("League")]
        public List<League> League { get; set; }
    }

    [XmlRoot("League")]
    public class League
    {
        [XmlElement(ElementName = "Id")]
        public int Id { get; set; }

        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }

        [XmlElement(ElementName = "Country")]
        public string Country { get; set; }

        [XmlElement(ElementName = "Historical_Data")]
        public string Historical_Data { get; set; }

        [XmlElement(ElementName = "Fixtures")]
        public string Fixtures { get; set; }

        [XmlElement(ElementName = "LiveScore")]
        public string Livescore { get; set; }

        [XmlElement(ElementName = "NumberOfMatches")]
        public int NumberOfMatches { get; set; }

        [XmlElement(ElementName = "LatestMatch")]
        public DateTime LatestMatch { get; set; }
    }

}
​
​

暂无
暂无

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

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