【问题标题】:c# Xpath with multiple namespacesc# Xpath 具有多个命名空间
【发布时间】:2012-08-15 15:56:37
【问题描述】:

谁能解释一下为什么这不起作用?

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

【问题讨论】:

  • 您是否收到错误消息?

标签: c# xml xpath


【解决方案1】:

您的其中一个命名空间有一个额外的尾部斜杠。试试

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

【讨论】:

  • 难以置信...我从它自己的 xml 文档中复制了它。那个愚蠢的斜线让我忙了3个小时! Tnx 伴侣!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多