C#XML序列化到对象 [英] C# Serialize XML to Object

查看:117
本文介绍了C#XML序列化到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化器的XML转换成一个对象。不过,我,遇到了一些麻烦。我已经经历了也许就张贴一个答案开通了网络,但我一直没能弄明白。请让我来解释一下:

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:

我有下面的XML,我想连载:

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>



即时通讯使用四个类捕获这个XML:

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()));

和我得到的错误是:

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!

问候,

鲍勃

推荐答案

第一个明显的错误是:

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



这当然应该是:

which should of course be:

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



不过,你也应该注意到,XML是大小写敏感的:

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

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

应该是:

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

相匹配的XML;同样

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

应该是:

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

和:

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

应该是:

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

另外,请注意,<玩家> 不可以 Import_Event - 它是活动部分。这使得生活变得复杂

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



除......那些日期/时间是不是有效的XML日期/时间 - 它们在格式错误。所以,你可能要像对待那些为字符串现在数据

最后,你的XML格式不正确 - 你有,没有关闭根元素

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

坦率地说,我并不感到惊讶串行不喜欢那样:)

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

这篇关于C#XML序列化到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆