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.
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.
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.
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).
You need to use the name()
function. Just name
alone will try to match an element named "name".
You want this:
//*[contains(name(),'mystring')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With