Get All node name in xml in silverlight

牧云@^-^@ 提交于 2019-12-19 12:21:32

问题


i created one xml like this

<names>
<name>
nixon
</name>
</names>

i want iterate each node name by foreach loop or any other loop in silverlight using xdocument.

how can i get this node name?


回答1:


You can use the DescendantsAndSelf() method of XElement to get all the nodes and their names.

foreach (XElement child in doc.Root.DescendantsAndSelf())
{
    Console.WriteLine(child.Name.LocalName);
}

DescendantsAndSelf() Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.




回答2:


With LinqToXml:

var xDoc = XDocument.Parse(mySmlString);
var names = xDoc.Root.Elements("name").Select(x=> x.Value.Trim()).ToArray();
foreach (var name in names)
{
    System.Console.WriteLine(name);
}


来源:https://stackoverflow.com/questions/14476744/get-all-node-name-in-xml-in-silverlight

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!