从序列化的WCF xml配置文件中选择节点

时间:2009-09-25 18:25:38

标签: c# xml xpath

自从我搞砸XPath并需要一些指导以来已经有好几年了。

这是xml片段:

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationObject xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" i:type="AlgorithmDataSourceConfiguration" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://www.foo.com/bar/2008/03">
  <ConfigurationSection z:Id="2">
    <Type z:Id="3">
      <Name z:Id="4">AlgorithmDataSource</Name>

CODE:

        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNamespaceManager mng = new XmlNamespaceManager(doc.NameTable);

        mng.AddNamespace("z", "http://schemas.microsoft.com/2003/10/Serialization/");

        XmlNodeList list = doc.SelectNodes("ConfigurationObject/ConfigurationSection /@z:Id=\"4\"",mng);
        foreach(XmlNode s in list)
        {
            Console.WriteLine(s.InnerText);
        }

该列表不包含任何节点?

您怎么看?

1 个答案:

答案 0 :(得分:0)

从您提供的xml片段,此xpath:

//ConfigurationObject/ConfigurationSection[@z:Id='2']

将选择第一个ConfigurationSection节点。