Search code examples
xmldocumentxmlnode

Search XmlNode by attribute


I have XmlDocument. I don't know XPath, I only know that this xml document has node named "h3" with attribute id="PortalName" and this attribute is unique for all xml document. How to find this node? I try:

        XmlNode xnList = doc.SelectSingleNode("h3[@id='PortalName']");

but it does not work because it search only in root node. How to search in whole document? Thanks


Solution

  • Fortunately that's pretty easy:

    XmlNode xnList = doc.SelectSingleNode("//h3[@id='PortalName']");
    

    The "//h3" means "any h3 element in the whole document". (See the abbreviated syntax in the XPath spec for more information.)