No Nodes Selected from Atom XML document using XPath?

前端 未结 2 1470
名媛妹妹
名媛妹妹 2020-12-11 04:33

I\'m trying to parse an Atom feed programmatically. I have the atom XML downloaded as a string. I can load the XML into an XmlDocument. However, I can\'t traver

相关标签:
2条回答
  • 2020-12-11 05:04

    While the C# implementation may allow default namespaces (I don't know), the XPath 1.0 spec doesn't. So, give "Atom" its own prefix:

    nsMngr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
    

    And change your XPath appropriately:

    XmlNode node = atom.SelectSingleNode("//atom:entry/atom:link/app:edited", nsMngr);
    
    0 讨论(0)
  • 2020-12-11 05:12

    Load XML from a string and lookup for any 'Errors/Error' nodes.

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlResult);            
    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
    nm.AddNamespace("ns", "http://somedomain.com/namespace1/2"); //ns - any name, make sure it is same in the below line
    
    XmlNodeList errors = xmlDoc.SelectNodes("/ns:*//ns:Errors/ns:Error", nm);       
    

    -Mathulan

    0 讨论(0)
提交回复
热议问题