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 9055781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:02:28+00:00 2026-06-16T14:02:28+00:00

I have the following c# method, that performs some operation on all nodes reachable

  • 0

I have the following c# method, that performs some operation on all nodes reachable from refNode, through xpath

void foo(XmlNode refNode, string xpath)
{
    XmlNodeList list=refNode.SelectNodes(xpath);
    //perform operation on each element of the list
}

One of the input xml that i’m getting is:

<A>
    <B>***
        <C>
                  <B>One</B>
            </C>
        <B>
                  <B>Two</B>
            </B>
    </B>
    <B>...</B>
    <B>...</B>
</A>

where i need to select a refNode <B> (marked ***) and pass it to foo() with an xpath that selects all descendent <B> nodes of refNode, but not nested inside any other <B> node

for example in the given input the result should contain:

1. <B>One</B>
2. <B><B>Two</B></B>

I have tried .//B which gives me 3 results and .//B[not(ancesotr::B)] which returns 0 results.

What Xpath should I use to get the desired result?

Edit

I can make changes to method foo, but not its signature. This method is a part of a library and is being used by few users. The input given above is just a specific instance, the user might also send node A as refnode and ask for all C nodes.

Edit2
@Dimitre Novatchev’s solution works for me if I can get the xpath of the refnode inside foo without changing its signature or if there is some way to specify this node, i.e. the node on which the xpath is being applied.

.//B[not(ancesotr::B) or ancesotr::B[1]=**this**]
  • 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-06-16T14:02:29+00:00Added an answer on June 16, 2026 at 2:02 pm

    Use this pure XPath 1.0 expression:

    $vrefNode/descendant::B[count(ancestor::B) - count($vrefNode/ancestor::B) = 1]
    

    where $vrefNode needs to be substituted (unless you can use variable references) with the XPath expression that selects the “reference node”.

    XSLT – based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "/*/B[1]/descendant::B[count(ancestor::B) - count(/*/B[1]/ancestor::B) = 1]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <A>
        <B>***
            <C>
                <B>One</B>
            </C>
            <B>
                <B>Two</B>
            </B>
        </B>
        <B>...</B>
        <B>...</B>
    </A>
    

    the XPath expression is evaluated and the selected elements by this evaluation are copied to the output:

    <B>One</B>
    <B>
    
       <B>Two</B>
    
    </B>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method that returns void and I need to use it
I have the following method that is replacing a pound sign from the file
I have the following static method that prints the data imported from a 40.000
I have an interface (called Subject ) that has the following method: public void
I have a method that performs some task with a timeout. I use the
I have the following method that is supposed to be a generic Save to
I have a method that contains the following (Java) code: doSomeThings(); doSomeOtherThings(); doSomeThings() creates
I have a method that I will use in the following contexts: 1. User
I have a program that makes use of the following method to get a
I have a simple mapview that has the following viewdidload method and didupdate to

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.