在XML文档中迭代子节点

4

我有以下XML文件结构:

<configuration>
 <Points>
    <Point1>
      <Url>net.tcp://10.1.1.144</Url>
      <Domain>10.1.1.144</Domain>
      <Secure>true</Secure>
      <UserName>flofy</UserName>
      <Password>Jojo</Password>
    </Point1>

    <Point2>
      <Url>net.tcp://10.1.1.22</Url>
      <Domain>10.1.1.22</Domain>
      <Secure>false</Secure>
      <UserName></UserName>
      <Password></Password>
    </Point2>
 </Points>
</configuration>

我想遍历所有点,我尝试了以下方法:
var doc = new XmlDocument();
doc.Load(@"C:\myXml.xml");
var nodes = doc.DocumentElement.SelectNodes("/configuration/Points");
foreach (XmlNode n in nodes)
{
    MessageBox.Show(n.Name);
}

但它只打印了Points,但我希望它能打印Point1Point2等等。
1个回答

3

您想要做的是...

foreach (XmlNode n in doc.SelectSingleNode("/configuration/Points").ChildNodes)
{
    MessageBox.Show(n.Name);
}

你的xpath查询仅选择“Points”节点,但你想迭代其子节点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接