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

  • Home
  • SEARCH
  • 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 3877784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:32:16+00:00 2026-05-19T22:32:16+00:00

I also think I’m confused about XPath usage. I’m new to C# and XPath,

  • 0

I also think I’m confused about XPath usage. I’m new to C# and XPath, so please be patient with me 😉

First, my XML file that I’m testing with:

<?xml version="1.0" encoding="ISO-8859-1"?>
<testroot>
    <testnode>
        <name>Test Node 1</name>
        <things>
            <thing>
                <number>One</number>
            </thing>
            <thing>
                <number>Two</number>
            </thing>
        </things>
    </testnode>
    <testnode>
        <name>Test Node 2</name>
        <things>
            <thing>
                <number>Three</number>
            </thing>
            <thing>
                <number>Four</number>
            </thing>
        </things>
    </testnode>
    <testnode>
        <name>Test Node 3</name>
        <things>
            <thing>
                <number>Five</number>
            </thing>
        </things>
    </testnode>
</testroot>

So first I want to get the “testnode” that contains a “name” I’m interested in. So I did the following, which worked correctly:

XmlNode testRoot = xmlDoc.DocumentElement.SelectSingleNode("/testroot/testnode[name=\"Test Node 1\"]");

Now I want to get all of the nodes under it that contain a “number” element. According to my reading, I should be able to do this:

XmlNodeList testNodes = testRoot.SelectNodes("number");

But that yields an empty list. The only way I got any results was to use //:

XmlNodeList testNodes = testRoot.SelectNodes("//number");

The problem is that seems to search all of the siblings and parents of the testRoot. When I print everything out I get every node in the file that contains “number”:

txtOutput.InnerHtml += "<p>" + testRoot.FirstChild.InnerText + "</p>";

foreach (XmlNode node in testNodes)
{
    txtOutput.InnerHtml += node.InnerText + "<br />";
}
Test Node 1
One
Two
Three
Four
Five

This behavior confuses me. Am I using XPaths improperly, or does it normally search from the absolute root no matter which XmlNode you start with?

  • 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-19T22:32:17+00:00Added an answer on May 19, 2026 at 10:32 pm

    You’re trying to find direct child nodes called number. There aren’t any of those – just one things element. If you want descendants, say so:

    XmlNodeList testNodes = testRoot.SelectNodes("descendant::number");
    

    The version with "//number" basically looks through all nodes in the document, hence your other result.

    Having said all this, if you’re using .NET 3.5 or higher I’d just use LINQ to XML and do all the querying in that. It’s a much nicer API, IMO 🙂

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

Sidebar

Related Questions

See also How does a WCF server inform a WCF client about changes? (Better
This is also a question that I asked in a comment in one of
I would like to know if ParseExact is faster than Parse. I think that
Suppose I have a method public Patient(int id) { ---- } that returns Patient
I think this is a new spin on an old question, but I'm completely
I'd like to speed up junit tests and I think about using forkMode=once .
I've got an unusual problem (well, I think that it is unusual). I need
Also, how do LEFT OUTER JOIN , RIGHT OUTER JOIN , and FULL OUTER
Also, where would I find them?
Navision is also known as Microsoft Dynamics NAV.

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.