Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:00:58+00:00 2026-05-22T01:00:58+00:00

I am new to xpath in C# and I am trying to select an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:00:58+00:00Added an answer on May 22, 2026 at 1:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to select specific values from a xml document using XPath. The
I am trying to select specific values from a xml document using XPath. The
I have a snippet of an xml file that looks like this: <?xml version=1.0
I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I'm trying to get the select list values using xpath because by id is
So I have some perl code that goes something like: use strict; use XML::XPath;
Here is the code that i am trying to implement $selector = xpath select
I'm new to jQuery. This would be no problem for me using XPath expressions,
<?php $feed = file_get_contents('http://thexmofo.wordpress.com/feed/'); $xml = new SimpleXMLElement($feed); $xml->registerXPathNamespace('media', 'http://thexmofo.wordpress.com/feed/'); $images = $xml->xpath('/rss/channel/item/media:content@url'); var_dump($images);
New to Regex. I want to validate to this format: Any character allowed, except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.