XmlNode.SelectSingleNode returns element outside current?

后端 未结 4 1983
我在风中等你
我在风中等你 2021-02-18 17:06

my problem is like this. Let\'s say i have xml like this


  
    Value1
    

        
相关标签:
4条回答
  • 2021-02-18 17:24

    the problem here is the XPath expression you are using, try it without the '//'. Like that:

    node.SelectSingleNode( "element3" );

    Read more here .

    0 讨论(0)
  • 2021-02-18 17:26

    The "//" is a global look up.

    What you'll need to do is get a list of all children

    XmlNodeList nodes = xmlDoc.SelectNodes("//Child");
    

    loop through that list and do a

    XmlNode node = nodes.SelectSingleNode("element3");
    

    This will return null if it's not there, and will step through every child looking.

    0 讨论(0)
  • 2021-02-18 17:30

    The XPath expression you have isn't what you want.

    Replace it with this:

    node.SelectSingleNode( "element3" ); 
    

    And you'll get the result you're looking for.

    0 讨论(0)
  • 2021-02-18 17:33

    The following work perfect when i want to run xpath on the specified node.

    XmlNodeList nodes = xmlDoc.SelectNodes(".//Child");
    
    0 讨论(0)
提交回复
热议问题