views:

35

answers:

1

Hi,

I want to deserialise an xml document with a number of same name nodes into an IList in c# I do not have control over the xml and therefore cant change it.

<root>
 <node1 name="" version="" />
 <node1 name="" version="" />
 <node1 name="" version="" />
 <node1 name="" version="" />
 <node2></node2>
 <node3></node3>
</root>

I have two classes Root and Node1 which look like:

[XmlRoot("root")]
public class Root
{
 public IList<Node1> Node1List { get; set; }

 [XmlElement("node2")]
 public string Node2 { get; set; }

 [XmlElement("node3")]
 public string Node3 { get; set; }
}

[XmlRoot("node1")]
public class Node1
{


 [XmlAttribute("name")]
 public string Name{ get; set; }

 [XmlAttribute("version")]
 public string Version{ get; set; }
}

Any Ideas how I can deserialise the xml so that all node1 elements are part of Node1List?

+2  A: 

Add XmlElement: (you may need to remove [XmlRoot("node1")])

[XmlElement("node1")]
public List<Node1> Node1List { get; set; }
SwDevMan81
I did have the [XmlElement] on my property. The problem is that the reflector blows up trying to deserialize to an interface. I.e. the IList. I changed this to a List and it works.
skyfoot