我正在尝试将 XML 序列化为对象。但是,我遇到了一些麻烦。我已经浏览了有关答案的在线帖子,但我无法弄清楚。请让我解释一下:
我有以下要序列化的 XML:
<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>
我使用四个类来捕获这个 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; }
}
我用来反序列化的代码是:
XmlSerializer serializer = new XmlSerializer(typeof(Import_Organization));
Import_RootObject ei = (Import_RootObject)serializer.Deserialize(new StringReader(sb.ToString()));
我得到的错误是:
There is an error in XML document (1, 2).
<Import_RootObject xmlns=''> was not expected.
有谁知道我在这里想念什么?希望有人能帮忙!
问候,
鲍勃