简体   繁体   中英

C# Serialize XML to Object

I am trying to serializer an XML into an object. I am however, having some trouble. I have gone through maybe postings online regarding an answer, but I haven't been able to figure it out. Please let me explain:

I have the below XML that I want to serialise:

<Import_RootObject>
 <Organizations>
  <Import_Organization OrgNr="xxxx">
   <Events>
    <Import_Event StartTime="2012-01-01 09:00:00" EndTime="2012-01-02 12:00:00">
    <Players>
     <Import_Player PersonNummer="1111" />
     <Import_Player PersonNummer="2222" />
     <Import_Player PersonNummer="3333" />
     <Import_Player PersonNummer="4444" />
    </Players>
   </Import_Event>
  </Events>
 </Import_Organization>
</Organizations>

Im using four classes to capture this XML:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

[XmlRoot("Import_RootObject")]
public class Import_RootObject
{
    [XmlArray("organizations")]
    [XmlArrayItem("organizations")]
    public List<Import_Organization> Organizations { get; set; }
}



using System;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Import_Organization
{
    [XmlAttribute("orgnr")]
    public string OrgNr { get; set; }

    [XmlArray("events")]
    [XmlArrayItem("events")]
    public List<Import_Event> Events { get; set; }

}



using System;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Import_Event
{
    [XmlAttribute("starttime")]
    public DateTime StartTime { get; set; }
    [XmlAttribute("endtime")]
    public DateTime EndTime { get; set; }

    [XmlArray("players")]
    [XmlArrayItem("players")]
    public List<Import_Player> Players { get; set; }

}



using System;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Import_Player
{
    [XmlAttribute]
    public string PersonNummer { get; set; }

}

The code I use to deserialize is:

XmlSerializer serializer = new XmlSerializer(typeof(Import_Organization));
Import_RootObject ei = (Import_RootObject)serializer.Deserialize(new StringReader(sb.ToString()));

And the error I'm getting is:

There is an error in XML document (1, 2).
<Import_RootObject xmlns=''> was not expected.

Does anyone know what I'm missing here? Hope someone can help out!

Regards,

Bob

The first obvious error is:

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

which should of course be:

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

However, you should also note that xml is case-sensitive:

[XmlArray("organizations")]
[XmlArrayItem("organizations")]

should be:

[XmlArray("Organizations")]
[XmlArrayItem("Import_Organization")]

to match the xml; likewise

[XmlArray("events")]
[XmlArrayItem("events")]

should be:

[XmlArray("Events")]
[XmlArrayItem("Import_Event")]

and:

[XmlArray("players")]
[XmlArrayItem("players")]

should be:

[XmlArray("Players")]
[XmlArrayItem("Import_Player")]

Additionally, note that <Players> is not a descendant of Import_Event - it is part of Events . This makes life a little complex.

We then note that your dates are not "starttime" / "endtime", so we can simplify to:

[XmlAttribute]
public DateTime StartTime { get; set; }
[XmlAttribute]
public DateTime EndTime { get; set; }

except... those date/times are not valid xml date/times - they are in the wrong format. So you might have to treat those as string data for now.

Finally, your xml is malformed - you have not closed the root element.

Frankly, I'm not surprised the serializer didn't like that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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