XML Array Element not assigning Attributes when deserialising (C#)

23 views Asked by At

I have a pre-existing XML structure which I want deserialised into C#.

The structure of the file is:

<SLOT_DEFINITION>
    <SLOT name="head" >
        <MESH model="head01.fbx" />
    </SLOT>
    <SLOT name="arm" >
        <MESH model="arm01.fbx" />
    </SLOT>
    <SLOT name="torso" >
        <MESH model="torso01.fbx" />
    </SLOT>
</SLOT_DEFINITION>

I've created some classes to represent this structure in C#:

[XmlRoot("SLOT_DEFINITION")]
public class SlotDefinition : List<Slot>
{

}

[XmlRoot("SLOT")]
[XmlType("SLOT")]
public class Slot : List<Mesh>
{
    [XmlAttribute("name")]
    public string Name { get; set; }
}

[XmlType("MESH")]
[XmlType("MESH")]
public class Mesh
{
    [XmlAttribute("model")]
    string ModelPath { get; set; }
}

But when I deserialise like this:

XmlSerializer serializer = new XmlSerializer(typeof(SlotDefinition));
StreamReader reader = new StreamReader(xmlPath);
SlotDefinition slotDefinition = (SlotDefinition)serializer.Deserialize(reader);

the elements of the SlotDefinition, and the Slot are populated, but the Name and ModelPath properties are not assigned their XMLAttribute values.

In fact, the leaf-object (Mesh) is getting its ModelPath set. I just had to give it the public attribute. But after I do this, the Slot class still doesn't get its attribute.

I want to avoid defining my SlotDefition as a field on some other class, decorating it with [XmlArrayItem], and deserialise that outer class. I also want to avoid writing custom deserialization logic since this seems like it should be a trivial structure.

0

There are 0 answers