1

Currently I'm working on some code that deserializes a XML file into an object. I have figured out most of the Attributes that I have to use to make this work except for one case, described below.

This is the XML document that I'm trying to Deserialize.

The pages-element is an array. The page-element is an object. The object-elements in the page are supposed to be in an array but they aren't when you take a look at the XML format. The amount of objects is variable though.

<document>
    <pages>
        <page>
            <object></object>
            <object></object>
            <object></object>
        </page>
        <page>
            <object></object>
        </page>
        <page>
            <object></object>
            <object></object>
        </page>
    </pages>
</document>

These are the classes that form the object that I'm trying to deserialize it to.

[XmlRoot("document")]
public class Document
{
    [XmlArrayItem("page", typeof(Page))]
    [XmlArray("pages")]
    public Page[] pages;
}

[XmlRoot("page")]
public class Page
{
    public Object[] objects;
}

[XmlRoot("object")]
public class Object
{

}

The question then is; what attributes do I need to enable the XmlSerializer class to deserialize the XML to a Document object?

MrWahloh
  • 11
  • 1
  • 3
    Your page array has two xml tags 1) Pages 2) Page. The object array has only one xml tag. So you need to add [XmlElement("object")]. The default for serialization for an array is two xml tags. – jdweng Feb 20 '20 at 15:47
  • 1
    @jdweng Well that was quick. I added [XmlElement("object"] to the array objects in the Page class and that seems to work. Thanks – MrWahloh Feb 20 '20 at 15:59

0 Answers0