I want to retrieve all the RULE nodes that have one or more attributes that contains a given string. Once i found them I need to replace the string with a new one.
I tested those XPath query but they do not work:
string xpathquery = "/FIREWALL/ETH/RULE[contains(@*,'" + toTest + "')]"; //DOES NOT WORK
string xpathquery = "/FIREWALL/ETH/RULE[contains(attribute::*,'" + toTest +"')]"; //DOES NOT WORK
The strange thing is that if I use @attributename or attribute::attributename it actually works. Where is the error?
I am using the contains function because there could be two different cases, let’s say I want to replace $HOST1 with $HOST2:
<RULE .... host="$HOST1"/> //NO PROBLEM
<RULE .... host="$HOST1:21"/> //I NEED TO REPLACE ONLY $HOST1, NOT THE ENTIRE STRING
I can’t just set the Value attribute of XmlNode but I need to modify the string. Actually I don’t know if XPath can handle this with a find and replace or something like this.
The query you’re using checks the concatenated text of all the attributes. The correct syntax is:
In other words, find a matching attribute and return its parent element. Of course, if what you want to do is to work with the attribute values, you can dispense with the
/..on the end, in which case you’ll be returned the actual attribute nodes and can just iterate through them modifying the values accordingly.