Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing xml to class, trouble with list<>

I have the following XML

<map version="1.0">
    <properties>
        <property name="color" value="blue" />
        <property name="size" value="huge" />
        <property name="texture" value="rugged" />
    </properties>
</map>

I am trying to write classes that I can deserialize this into, this is what I have:

[XmlRoot("map")]
public class MyMap
{
    [XmlAttribute("version")]
    public decimal Version { get; set; }
    [XmlElement("properties")]
    public List<MyProperty> Properties { get; set; }
}

public class MyProperty
{
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlAttribute("value")]
    public string Value { get; set; }
}

The problem is that I cant seem to read the property list, I just get one entry and it has null in both Name and Value.

Are there some magic attributes I need to set to get this to work?

like image 387
Toodleey Avatar asked Apr 24 '12 18:04

Toodleey


1 Answers

You should change MyMap as below. XmlArray and XmlArrayItem are the magic attributes

[XmlRoot("map")]
public class MyMap
{
    [XmlAttribute("version")]
    public decimal Version { get; set; }
    [XmlArray("properties")]
    [XmlArrayItem("property")]
    public List<MyProperty> Properties { get; set; }
}
like image 130
L.B Avatar answered Sep 18 '22 18:09

L.B