简体   繁体   中英

How to Deserialize xml with muliple matching nodes to array in c#

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?

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

[XmlElement("node1")]
public List<Node1> Node1List { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM