XPATHS and Default Namespaces

后端 未结 5 2068
囚心锁ツ
囚心锁ツ 2020-11-28 12:34

What is the story behind XPath and support for namespaces? Did XPath as a specification precede namespaces? If I have a document where elements have been given a default na

5条回答
  •  渐次进展
    2020-11-28 13:38

    I tried something similar to what palehorse proposed and could not get it to work. Since I was getting data from a published service I couldn't change the xml. I ended up using XmlDocument and XmlNamespaceManager like so:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlWithBogusNamespace);            
    XmlNamespaceManager nSpace = new XmlNamespaceManager(doc.NameTable);
    nSpace.AddNamespace("myNs", "http://theirUri");
    
    XmlNodeList nodes = doc.SelectNodes("//myNs:NodesIWant",nSpace);
    //etc
    

提交回复
热议问题