XML and Loose Array Deserialization Elements

So, I work with some XML files, which I think are most likely poorly 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> 

Now I would like to create a class that has these three types: Root, ArrayType1 and ArrayType2, but I would like to get two lists in Root, one of which contains a collection of ArrayType1 elements, and one containing a collection of ArrayType2, but it seems that these elements must have some kind of root, for example, I know how to deserialize the following is 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> 

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

Can this XML serializer allow this at all?

+3
source share
1 answer

Isn't it like that:

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

?

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

 xsd foo.xml xsd foo.xsd /classes 

and look at the generated foo.cs

+11
source

All Articles