XML反序列化和适度宽松的数组项 [英] XML Deserialization and Loose Array Items

查看:169
本文介绍了XML反序列化和适度宽松的数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用,我相信最有可能形成严重的一些XML文件的工作,而我试图找出如何,如果可以使用XmlSerializer的这个XML反序列化为一个逻辑业务对象。比方说,我有以下XML文件:

So I'm working with some XML files that I believe are most likely badly formed, and I'm trying to figure out how and if I can use the XmlSerializer to deserialize this XML into a logical business object. Let's say I have the following XML file:

<Root>
   <ArrayType1 Name="Bob"/>
   <ArrayType1 Name="Jim"/>
   <ArrayType2 Name="Frank">
      <SubItem Value="4"/>
   </ArrayType2>
   <ArrayType2 Name="Jimbo">
      <SubItem Value="2"/>
   </ArrayType2>
</Root>

现在我想创建一个具有这三种类型,根,ArrayType1和ArrayType2一类,但我想获得两个列表中的根,一个包含的ArrayType1项的集合,以及一个包含集合ArrayType2的项目,但似乎这些项目需要有某种根源,比如,我知道如何反序列化下面就好了:

Now I'd like to create a class that has these three types, Root, ArrayType1 and ArrayType2, but I'd like to get two lists in Root, one containing a collection of ArrayType1 items, and one containing a collection of ArrayType2 items, but it seems that these items need to have some sort of root, for example, I know how to deserialize the following just fine:

<Root>
   <ArrayType1Collection>
      <ArrayType1 Name="Bob"/>
      <ArrayType1 Name="Jim"/>
   </ArrayType1Collection>
   <ArrayType2Collection>
      <ArrayType2 Name="Frank">
         <SubItem Value="4"/>
      </ArrayType2>
      <ArrayType2 Name="Jimbo">
         <SubItem Value="2"/>
      </ArrayType2>
   </ArrayType2Collection>
</Root>

但我怎么会反序列化这一点没有周围的数组类型元素#母公司将ArrayType#集合元素?

But how would I deserialize this without the parent ArrayType#Collection elements surrounding the ArrayType# elements?

将XML序列化甚至让这个呢?

Will the XML Serializer even allow this at all?

推荐答案

是不是只是:

[Serializable]
public class Root {
    [XmlElement("ArrayType1")]
    public List<ArrayType1> ArrayType1 {get;set;}

    [XmlElement("ArrayType2")]
    public List<ArrayType2> ArrayType2 {get;set;}
}

另外,只要把XML转换的文件(foo.xml),并使用:

Alternatively, just put the xml into a file ("foo.xml") and use:

xsd foo.xml
xsd foo.xsd /classes

和查看生成的foo.cs

and look at the generated foo.cs

这篇关于XML反序列化和适度宽松的数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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