2

我有一个问题:我有一个.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
    <item>
      <id>2</id>
      <title>Live Test Event</title>
      <tournament>
        <id>6</id>
        <name>Test tournament</name>
        <property>
          <id>4</id>
          <name>Test property</name>
          <sport>
            <id>3</id>
            <name>Test Sport</name>
          </sport>
        </property>
      </tournament>
      <updatedAt>2018-01-22T11:11:44+0000</updatedAt>
      <startDate>2017-01-01T00:00:00+0000</startDate>
      <endDate>2018-12-31T00:00:00+0000</endDate>
    </item>
    ...
</items>

我也有一堂课:

public class StreamingEvents
{
    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Event> Events { get; set; }
}

为了反序列化这个文件,我需要在类声明之前放置一个 XmlRoot 属性。但问题是我的 .xml 中没有 root 属性。我只有一个数组“项目”。而且我需要使用 XmlRoot attr 来反序列化而不会出错。有人可以帮助我吗?

4

3 回答 3

1

没有根节点的 XML 在技术上不是 XML。这是一个要求,您甚至必须手动添加它

于 2018-01-26T16:31:40.890 回答
1

我相信<items>是xml的根。在这种情况下,您的课程应如下所示,以便成功反序列化您的 xml

[XmlRoot("items")]
public class StreamingEvents
{
    [XmlElement("item")]
    public List<Event> Events { get; set; }
}
于 2018-01-26T16:55:53.483 回答
0

在这种情况下不要使用 XmlArray。对 Items 和 Item 使用两个单独的类

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



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

            XmlReader reader = XmlReader.Create(FILENAME);

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

            StreamingEvents events = (StreamingEvents) serializer.Deserialize(reader);

        }
    }
    [XmlRoot("items")]
    public class StreamingEvents
    {
        [XmlElement("item")]
        public List<Event> Events { get; set; }
    }
    public class Event
    {
        IFormatProvider provider = CultureInfo.InvariantCulture;
        public int id { get; set; }
        public string title { get; set; }
        private DateTime _updatedAt { get; set; }

        public string updatedAt
        {
            get { return _updatedAt.ToString(); }
            set { _updatedAt = DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss+ffff", provider); }
        }
        private DateTime _startDate { get; set; }
        public string startDate
        {
            get { return _startDate.ToString(); }
            set { _startDate = DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss+ffff", provider); }
        }
        private DateTime _endDate { get; set; }
        public string endDate
        {
            get { return _endDate.ToString(); }
            set { _endDate = DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss+ffff", provider); }


        }
        public Tournament tournament { get; set; }
    }
    public class Tournament
    {
        public int id { get; set; }
        public string name { get; set; }
        public Property property { get; set; }
    }
    public class Property
    {
        public int id { get; set; }
        public string name { get; set; }
        public Sport sport { get; set; }
    }
    public class Sport
    {
        public int id { get; set; }
        public string name { get; set; }
    }


}
于 2018-01-26T17:41:20.890 回答