[英]C# Serialize XML to Object
I am trying to serializer an XML into an object. 我正在尝试将XML序列化为对象。 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: 我有以下要序列化的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>
Im using four classes to capture this XML: 我使用四个类来捕获这个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: 但是,您还应注意xml区分大小写:
[XmlArray("organizations")]
[XmlArrayItem("organizations")]
should be: 应该:
[XmlArray("Organizations")]
[XmlArrayItem("Import_Organization")]
to match the xml; 匹配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
. 另外,请注意
<Players>
不是 Import_Event
的后代 - 它是Events
一部分。 This makes life a little complex. 这使生活变得有点复杂。
We then note that your dates are not "starttime" / "endtime", so we can simplify to: 然后我们注意到您的日期不是“starttime”/“endtime”,因此我们可以简化为:
[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. 除了......那些日期/时间无效xml日期/时间 - 它们格式错误。 So you might have to treat those as
string
data for now. 所以你现在可能不得不将它们视为
string
数据。
Finally, your xml is malformed - you have not closed the root element. 最后,您的xml格式错误 - 您尚未关闭根元素。
Frankly, I'm not surprised the serializer didn't like that :) 坦率地说,我并不觉得序列化器不喜欢那样:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.