2

I have a question: I have an .xml file

<?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>

Also I have a class:

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

And to deserialize this file I need to put an XmlRoot attribute before class declaration. But the problem is that I don't have the root attribute in my .xml. I have only an array "items". And I need to use XmlRoot attr to deserialize without errors. Could anybody help me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oleg Kopot
  • 23
  • 3
  • did u try https://stackoverflow.com/questions/1556874/user-xmlns-was-not-expected-deserializing-twitter-xml ? – Daniel B Jan 26 '18 at 16:58

3 Answers3

1

XML without a root node is technically not XML. It's a requirement, you have to add it even manually

KHL
  • 455
  • 2
  • 12
1

I believe <items> is the root of the xml. In that case, your class should be as below in order to deseralize your xml successfully

[XmlRoot("items")]
public class StreamingEvents
{
    [XmlElement("item")]
    public List<Event> Events { get; set; }
}
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
0

Don't use XmlArray in this case. Use Two separate classes for Items and 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; }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20