[英]C# Deserialize XML elements with attributes into List
Here's the XML: 这是XML:
<xml id = "1234">
<connect id="2"/>
<connect id="1"/>
<connect id="21"/>
<connect id="3"/>
<connect id="7"/>
</xml>
Currently I am doing this: 目前,我正在这样做:
public class xml
{
//Constructor
[XmlAttribute ("id")]
public uint id;
[XmlElement ("connect")]
public List<Connection> Connections { get; set; }
//Deserializer
}
public class Connection
{
[XmlAttribute ("id")]
public uint id { get; set; }
}
The goal is to get rid of the Connection class entirely and Deserialize the xml straight into: 目标是完全摆脱Connection类并将xml反序列化为:
List<uint> connections;
First, your XML is not valid, i guess it's just a typo - there no end tag for "connect"
.首先,您的XML无效,我想这只是一个错字-没有用于 "connect"
结束标记。
I recommend you to use linq XDocument
. 我建议您使用linq
XDocument
。
Then it's easy: 然后很简单:
XDocument xdoc = XDocument.Parse(xml);
List<uint> list = xdoc
.Descendants("connect")
.Select(node => uint.Parse(node.Attribute("id").Value))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.