How do I use XPath with a default namespace with no prefix?

后端 未结 6 1477
广开言路
广开言路 2020-12-13 13:16

What is the XPath (in C# API to XDocument.XPathSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?



        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 13:51

    You need to use an XmlNamespaceManager as follows:

       XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
       XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
       mgr.AddNamespace("df", "lcmp");
       foreach (XElement myNode in doc.XPathSelectElements("configuration/df:MyNode", mgr))
       {
           Console.WriteLine(myNode.Attribute("attr").Value);
       }
    

提交回复
热议问题