遍历XML文档中的子节点

时间:2018-09-26 14:16:45

标签: c# xml

我具有以下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 个答案:

答案 0 :(得分:3)

你想做..

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

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

相关问题