Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select nodes where node name contains "mystring"

I need to get XmlNodeList where node name contains "mystring"

XML

    <?xml version="1.0" encoding="utf-8"?>
<root>
  <node1>
    node1 value
  </node1>
  <node2_mystring>
    node2 value
  </node2_mystring>
  <node3>
    node3 value
  </node3>
  <node4_mystring>
    node 4 value
  </node4_mystring>
</root>

Desired output is

<?xml version="1.0" encoding="utf-8"?>
<root>
  <node2_mystring>
    node2 value
  </node2_mystring>
  <node4_mystring>
    node 4 value
  </node4_mystring>
</root>

I tried something like XmlNodeList mystringElements = xmlDocument.SelectNodes(@"//*[contains(name,'mystring')]");

But it returns zero node. What should I put in XPath query to achieve this.

like image 798
afin Avatar asked Apr 22 '10 13:04

afin


People also ask

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

What does node () do in XPath?

node() matches any node (the least specific node test of them all) text() matches text nodes only. comment() matches comment nodes. * matches any element node.

What is node set in XPath?

A node set is a set of nodes. When you write an XPath expression to return one or more nodes, you call these nodes a node set. For example, if you use the following expression to return a node called title , you will have a set of nodes all called title (assuming there's more than one record).


1 Answers

You need to use the name() function. Just name alone will try to match an element named "name".

You want this:

//*[contains(name(),'mystring')]
like image 129
Welbog Avatar answered Nov 06 '22 23:11

Welbog