我有当前的xml文件:
<?xml version="1.0"?>
<Master>
<Child1>
<Display>Some things here</Display>
<Link>http://google.ca</Link>
<Description>Desc</Description>
<Image>http://google.ca</Image>
</Child2>
</Master>
我已经找到了如何使用doc.SelectSingleNode("Master/Child1/Link").InnerText;
但是现在,我需要一种方法来列出每个孩子(就像Child1一样,有的方式比所有子节点都要多,比如链接,显示....)
我尝试过很多东西,但我在网上找到的只是如何从<Master name="Name Here"/>
获取“名字”
另外,我需要它作为一个字符串(能够将其打印到控制台而不会获得System.xml.XmlNode
)
感谢您的帮助。
答案 0 :(得分:1)
在xpath中*
匹配任何节点。
var nodes = doc.SelectNodes("Master/*/Link");
foreach (XmlNode node in nodes)
Console.WriteLine(node.InnerText);
var nodes = doc.SelectNodes("Master/*");
foreach (XmlNode node in nodes)
Console.WriteLine(node.Name);