How do I get an IXmlNamespaceResolver

前端 未结 3 647
一向
一向 2021-01-11 12:57

I\'m trying to call the XElement.XPathSelectElements() overload that requires an IXmlNamespaceResolver object. Can anyone show me how to get (or make) an IXmlNamespaceResolv

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 13:41

    You can use an XmlNamespaceManager that implements that interface

    Use the constructor which takes an XmlNameTable, passing into it an instance of System.Xml.NameTable via new NameTable(). You can then call AddNamespace function to add namespaces:

    var nsMgr = new XmlNamespaceManager(new NameTable());
    nsMgr.AddNamespace("ex", "urn:example.org");
    nsMgr.AddNamespace("t", "urn:test.org");
    doc.XPathSelectElements("/ex:path/ex:to/t:element", nsMgr);
    

提交回复
热议问题