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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:30+00:00 2026-06-18T10:27:30+00:00

I have an xml like the following: <return> <exams> <remove> </remove> <add> <exam errorCode=0

  • 0

I have an xml like the following:

<return>
  <exams>
    <remove>
    </remove>
    <add>
        <exam errorCode="0" examRef="1" />
    </add>
    <add>
        <exam errorCode="0" examRef="1" />
        <exam errorCode="0" examRef="1" />
    </add>
  </exams>
</return>

and I am building a utility that can differentiate between each node by its extracting its ancestor hierarchy. e.g:

return[0].exams[0].add[0].exam[0] //This indicates the first exam node in the first add element.
return[0].exams[0].add[1].exam[0] //This indicates the first exam node in the second add element.
return[0].exams[0].add[1].exam[1] //This indicates the second exam node in the second add element.

and so on. The code that I have so far is:

    private string GetAncestorNodeAsString(XElement el)
    {
        string ancestorData = string.Empty;

        el.Ancestors().Reverse().ToList().ForEach(anc =>
        {
            if (ancestorData == string.Empty)
            {
                ancestorData = String.Format("{0}[0]", anc.Name.ToString());
            }
            else
            {
                ancestorData = String.Format("{0}.{1}[0]", ancestorData, anc.Name.ToString());
            }
        });

        if (ancestorData == string.Empty)
        {
            ancestorData = el.Name.ToString();
        }
        else
        {
            ancestorData = String.Format("{0}.{1}[0]", ancestorData, el.Name.ToString());
        }
        return ancestorData;
    }

This code returns something like:

return[0].exams[0].add[0].exam[0] //zeros here are hardcoded in the code and I need some mechanism to get the position of each of the element in the xml.

I can build up position of elements like:

            var elements = el.Elements().Select((e, index) => new
            {
                node = e,
                position = index
            });

but this will only give me position of only immediate child elements within an element. I need to identify all ancestors and its position in the xml.

Can anyone help please?

  • 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-18T10:27:31+00:00Added an answer on June 18, 2026 at 10:27 am

    Here is method which will return index of element:

    private int GetElementIndex(XElement e)
    {
        return e.NodesBeforeSelf().OfType<XElement>().Count(x => x.Name == e.Name);
    }
    

    And your modified code. Keep in mind – I use AncestorsAndSelf to use single loop. Also I avoided creating list of elements. And used StringBuilder to aggregate result and avoid creation of strings.

    private static string GetAncestorNodeAsString(XElement e)
    {
        return e.AncestorsAndSelf().Reverse()
                .Aggregate(
                   new StringBuilder(),
                   (sb, a) => sb.AppendFormat("{0}{1}[{2}]", 
                              sb.Length == 0 ? "" : ".", a.Name, GetElementIndex(a)),
                   sb => sb.ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have XML that looks like the following: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <ObjectList> <object
I have XML that looks like the following. <CONTAINER> <SEARCHFOR /> <ITEM/> <ITEM/> <ITEM/>
sI have an XML file that looks like the following... <a> <b> <version>1.0</version> <c>
I have an xml like the following <DataCollection> <Data> <Name>John</Name> <Age>30</Age> </Data> ... more
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>
If I have some xml containing things like the following mediawiki markup: ...collected in
Lets say I have a xslt stylesheet like the following: <?xml version=1.0 encoding=utf-8?> <xsl:stylesheet
I have an XML structure like the following: <Root> <Base> <Greeting>Base Hello!</Greeting> <SpecialMessage>Base Special
I have the following XML: <ns:response xmlns:ns=http://example.com xmlns:ax=http://example.com/xsd > <ns:return type=mytype> <ax:roleID>1</ax:roleID> <ax:roleName>ADM</ax:roleName> </ns:return>
I have the following xml that I need to deserialise to an object: <animals>

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.