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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:03:56+00:00 2026-06-13T08:03:56+00:00

My XML file consists of a structure that resembles something like the following: <root>

  • 0

My XML file consists of a structure that resembles something like the following:

<root>
    <Manager name="1">
        <Manager name="2">
            <Employee name="3">
        </Manager>
        <Manager name="abe">
        </Manager>
        <Employee name="4">
        <Employee name="5">
    </Manager>
</root>

The XML feeds a treeview, and depending on where in the treeview a user clicks, I either want to retrieve the Employee clicked on (which is easy, as I can use treeview.SelectedNode), or otherwise in case the click was on the root node, or a manager node, the first employee under the manager.

I.e.

  • Clicking on root should show details of Employee 4 (the first Employee record is directly under Manager 1 directly under root).
  • Clicking on Manager 1 also should show Employee 4.
  • Clicking Manager 2 should show Employee 3.
  • Clicking Manager Abe yields no results.
  • Employee 5 only shows when there is a click directly on that employee.

It could also be possible that Manager 1 does not have any direct employees under him. In that case, clicking on root should yield the first Employee under the first manager with employees. So if we assume Employee 4 and Employee 5 were not under Manager 1, clicking on root would yield Employee 3.

I tried using some different variants of Element, Elements, Descendant and Descendants, and am a bit stuck.

I suppose that I could write scenarios for every individual combination (I.e. rootClicked, managerClicked and employeeClicked), which is what I did originally, but I’m looking for something that will hopefully be easier to maintain, code-wise.

I had good hopes that using root.Element(“Employee”) would help, but that threw a Could not find an implementation of the query pattern for source type 'System.Xml.Linq.XEelement'. 'select' not found error.

Would anyone be able to supply me with that little nudge I need to solve my issue?

  • 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-13T08:03:57+00:00Added an answer on June 13, 2026 at 8:03 am

    Using XPath:

    ClickedNode.XPathEvaluate
        ("self::Employee | self::Manager/Employee[1] | self::root/Manager[1]/Employee[1]")
    

    Update:

    Answer to the edited question:

    Use:

     ClickedNode[self::Employee]
    |
     ClickedNode[not(self::Employee)]/descendant-or-self::Manager[Employee][1]/Employee[1]
    

    This selects either:

    a. The clicked node, if it is an `Employee’

    Or:

    b. The first child Employee of the first desendant-or-self of the clicked node, that is a manager and has a child Employee

    And, if ClickedNode is an XElement (or XNode), then do:

      ClickedNode.XPathEvaluate
            (self::*[self::Employee]
          | self::node()[not(self::Employee)]
                           /descendant-or-self::Manager[Employee][1]/Employee[1]
            )
    

    Finally, here is a complete C# code:

    A static class named TestLinqXpath:

    using System.Xml.Linq;
    using System.Xml.XPath;
    
    namespace TestLinqXpath
    {
        public  static class TestLinqXpath
        {
            public static XElement SelectNearestDescendantEmployee(XElement clicked)
            {
                string Expr =
    @"(self::*[self::Employee]
     | self::node()[not(self::Employee)]
               /descendant-or-self::Manager[Employee][1]/Employee[1]
       )";
                XElement result = clicked.XPathSelectElement(Expr);
    
                return result;
            }
        }
    }
    

    and extensive test:

    using System;
    using System.Xml.Linq;
    using System.Xml.XPath;
    
    namespace TestLINQ_Xml
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test();
            }
    
            static void Test()
            {
                string xml = 
    @"<root>
        <Manager name='1'>
            <Manager name='2'>
                <Employee name='3'/>
            </Manager>
            <Manager name='abe'>
            </Manager>
            <Employee name='4'/>
            <Employee name='5'/>
        </Manager>
    </root>";
                XElement top = XElement.Parse(xml);
    
                XElement cliked1 = top;
                XElement res1 = TestLinqXpath.TestLinqXpath.SelectNearestDescendantEmployee(cliked1);
                Console.WriteLine(res1.ToString());
    
                XElement cliked2 = top.XPathSelectElement("Manager[@name='1']");
                XElement res2 = TestLinqXpath.TestLinqXpath.SelectNearestDescendantEmployee(cliked2);
                Console.WriteLine(res2.ToString());
    
                XElement cliked3 = top.XPathSelectElement(".//Manager[@name='2']");
                XElement res3 = TestLinqXpath.TestLinqXpath.SelectNearestDescendantEmployee(cliked3);
                Console.WriteLine(res3.ToString());
    
                XElement cliked4 = top.XPathSelectElement(".//Manager[@name='abe']");
                XElement res4 = TestLinqXpath.TestLinqXpath.SelectNearestDescendantEmployee(cliked4);
                Console.WriteLine((res4 != null) ? res4.ToString() : "null");
    
                XElement cliked5 = top.XPathSelectElement(".//Employee[@name='5']");
                XElement res5 = TestLinqXpath.TestLinqXpath.SelectNearestDescendantEmployee(cliked5);
                Console.WriteLine(res5.ToString());
            }
        }
    }
    

    When this test is run, the wanted, correct results are produced:

    <Employee name="4" />
    <Employee name="4" />
    <Employee name="3" />
    null
    <Employee name="5" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have a file that consists of concatenated valid XML documents. I'd like to
I have a simple XML file created in R that consists of the following
I have a large XML file (converted to JSON) that consists of multiple repeating
I have a file, which is in XML format (consists just of root start
My application consists of data downloaded from an XML file. data contains short text
Hi I have big xml file which has a structure similar to the one
I have an XML file structured like this <serieslist> <series sid=123> <title type=main>Series 123
I'm working on a XSLT transformation that consists on merging two XML and then
I have a simple C# Console App that reads in an XML file specified

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.