C# Using XPATH to select specific element with known values, then delete

FeatherKing picture FeatherKing · May 10, 2011 · Viewed 7.8k times · Source

I am new to xpath in C# and I am trying to select an element that has two specific values. This is what the XML format looks like

<?xml version="1.0" encoding="utf-8"?>
<Manager>
  <SSH>
    <Tunnels>
      <Port>
        <Local>443</Local>
        <Remote>443</Remote>
      </Port>
      <Port>
        <Local>5432</Local>
        <Remote>5432</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>21</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>22</Remote>
      </Port>
    </Tunnels>
  </SSH>
</Manager>

I was trying to select a 'Port' that had the values from a previous form so i can delete that specific entry from the xml. This was the code i was using:

        //remove children from selected
        XmlNode _xmlTunnel = _xml.SelectSingleNode("/Manager/SSH/Tunnels/Port[Local=" + _local + "] | /Manager/SSH/Tunnels/Port[Remote=" + _remote + "]");
        MessageBox.Show("Local " + sshList.SelectedItems[0].Text + " Remote " + sshList.SelectedItems[0].SubItems[1].Text +"\n\n" + _xmlTunnel.InnerText);
        _xmlTunnel.RemoveAll();

        //remove all empties
        XmlNodeList emptyElements = _xml.SelectNodes(@"//*[not(node())]");
        for (int i = emptyElements.Count -1; i >= 0; i--) {
        emptyElements[ i ].ParentNode.RemoveChild(emptyElements[ i ]); }

This code works fine until I have two Ports with the same Local Value. It always selects the first element that it comes to (i.e. Local=19 and Remote=21, even if you try to select the node where Local=19 and Remote=22). I tried switching the xpath expression to 'and' instead of '|' in the SelectSingleNode method but that errors out with a "Expression must evaluate to a node-set". Which makes me think that I am evaluating out to a boolean when I use 'and'.

Is the better way to do this by a loop where a select the first element and loop until the second one matches? As I said before i do not have much experience with xpath/xml expressions in C#, perhaps there is a better way. If it helps I am using windows forms and .net 4.0, in this form the port values fill a two column list view in detail view.

Answer

Alexei Levenkov picture Alexei Levenkov · May 10, 2011

You need to "and" 2 conditions on Port node like:

"/Manager/SSH/Tunnels/Port[Local=" + _local + " and  Remote=" + _remote + "]"

In your case you are doing union of 2 sets where Local=19 and another where Remote=21.