循环访问 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等。

你想做..

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

您的 xpath 查询仅选择节点"点",但您希望迭代其子节点。

最新更新