[英]LINQ to XML: Project to a List<string>
Using LINQ to XML, how can I project the following XML data into a List<string>
with the values "Test1", "Test2" and "Test3". 使用LINQ to XML,如何将以下XML数据投影到List<string>
,其值为“Test1”,“Test2”和“Test3”。
<objectlist>
<object code="Test1" />
<object code="Test2" />
<object code="Test3" />
</objectlist>
I have the XML available in a string: 我有一个字符串中的XML:
XDocument xlist = XDocument.Parse(xmlData);
Thanks 谢谢
var query = from node in xlist.Root.Elements("object")
select node.Attribute("code").Value
var result = query.ToList();
Or, with extension method syntax: 或者,使用扩展方法语法:
var query = xlist.Root.Elements("object")
.Select(node => node.Attribute("code").Value)
.ToList()
var xDoc = XDocument.Parse(xml);
List<string> codes = xDoc.Descendants("object")
.Select(o => o.Attribute("code").Value)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.