XmlNode.SelectSingleNode syntax to search within a node in C#

前端 未结 2 685
醉梦人生
醉梦人生 2021-01-02 01:16

I want to limit my search for a child node to be within the current node I am on. For example, I have the following code:

XmlNodeList myNodes = xmlDoc.Docum         


        
相关标签:
2条回答
  • 2021-01-02 01:38

    Actually, the problem relates to XPath. XPath syntax // means you select nodes in the document from the current node that match the selection no matter where they are

    so all you need is to change it to

    myNode.SelectSingleNode(".LastName")
    
    0 讨论(0)
  • 2021-01-02 01:39

    A leading // always starts at the root of the document; use .// to start at the current node and search just its descendants:

    XmlNode lastnameNode = myNode.SelectSingleNode(".//LastName");
    
    0 讨论(0)
提交回复
热议问题