XmlSerialization in C # - an array of root elements

I would like to de-serialize an XML document with an array of type = root node. This xml structure is as follows:

<?xml version="1.0" encoding="UTF-8"?> <parties type="array"> <party type="Person"> <id>1</id> <lastname>Smith</lastname> <firstname>Peter</firstname> ... </party> <party type="Person"> <id>2</id> <lastname>Smith</lastname> <firstname>Sarah</firstname> ... </party> <parties type="array"> 

C # code looks like this:

 [XmlRootAttribute("parties", Namespace = "", IsNullable = false)] public class Parties { private ArrayList contacts = new ArrayList(); public Parties() { } [XmlArray("parties"), XmlArrayItem("party", typeof(Person))] public ArrayList Contacts { get { return contacts; } set { contacts = value; } } } 

The result is xml output:

 <?xml version="1.0" encoding="utf-8"?> <parties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <parties> <party> <id>0</id> <lastname>Smith</last-name> <firstname>Peter</first-name> </party> </parties> </parties> 

The problem is that now I have 2 tags. How to specify array type for root element? Any ideas how to fix this without changing this xml schema?

+4
source share
1 answer

Try the following:

 [XmlElement("party")] public ArrayList Contacts { get { return contacts; } set { contacts = value; } } 
+6
source

All Articles