反序列化需要XmlRoot属性,但在xml文件中没有root属性

时间:2018-01-26 16:20:05

标签: c# xml deserialization xmlroot

我有一个问题:我有.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属性。我只有一个阵列&#34; items&#34;。我需要使用XmlRoot attr进行反序列化而不会出错。有人能帮助我吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我相信catch是xml的根。在这种情况下,您的类应如下所示,以便成功解除您的xml

<items>

答案 2 :(得分:0)

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

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


}