[英]How to get value from xml element using Linq to xml concept in wpf
[英]How to get Element name in wpf using linq to xml concept?
<Contacts>
<Node>
<ID>123</ID>
<Name>ABC</Name>
</Node>
<Node>
<ID>124</ID>
<Name>DEF</Name>
</Node>
</Contacts>
在這里,我想獲取元素名稱(標簽名稱),如Contacts ,* Node *, ID ,* Name *使用linq到xml概念。我應該怎么做。
嘗試這個:
var xml = @"<Contacts>
<Node>
<ID>123</ID>
<Name>ABC</Name>
</Node>
<Node>
<ID>124</ID>
<Name>DEF</Name>
</Node>
</Contacts>";
var xdoc = XDocument.Parse(xml);
var namelist = xdoc.Descendants()
.Select(i => i.Name.ToString())
.Distinct()
.ToList();
你會得到:
{"Contacts","Node","ID","Name"}
如果要獲取第一個節點的名稱列表
var nodes = xdoc.Descendants(XName.Get("Node"));
var firstNode = nodes.First();
var namelist = firstNode.Descendants()
.Select(i => i.Name.ToString())
.Distinct()
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.