c#具有多个名称空间的Xpath

时间:2012-08-15 15:56:38

标签: c# xml xpath

有人可以解释一下,为什么这不起作用?

XmlDocument xmlDoc = new XmlDocument();                         xmlDoc.Load( “Bing2_ss.xml”);

                    //Instantiate an XmlNamespaceManager object. 
                    XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
                    //Add the namespaces used in feed to the XmlNamespaceManager.
                    //xmlnsManager.AddNamespace("base", "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite");
                    xmlnsManager.AddNamespace("m",   "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
                    xmlnsManager.AddNamespace("d",   "http://schemas.microsoft.com/ado/2007/08/dataservices/");
                    xmlnsManager.AddNamespace(String.Empty, "http://www.w3.org/2005/Atom");

                    XmlNodeList nodeList;
                    XmlElement root = xmlDoc.DocumentElement;
                    nodeList = root.SelectNodes("//m:properties/d:WebTotal", xmlnsManager);
                    foreach (XmlNode nd in nodeList)
                    {
                        System.Console.WriteLine(nd.InnerText);
                    }

这是原始的xml:http://gezond-afslanken.info/bing2_ss.xml

1 个答案:

答案 0 :(得分:2)

您的某个命名空间上有一个额外的尾部斜杠。尝试

xmlnsManager.AddNamespace("d","http://schemas.microsoft.com/ado/2007/08/dataservices");