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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:06:15+00:00 2026-06-07T08:06:15+00:00

Walking an XML document with XDocument using descendants how would I determine I am

  • 0

Walking an XML document with XDocument using descendants how would I determine I am at the last child of the parent?

XDocument xmlDoc = XDocument.Parse(xml)

  foreach (var element in xmlDoc.Descendants())
        {
           //Need to determine the last child of the parent and do something here. 
        }

Example of XML and what is expected to be returned. I need to dynamically walk the document and not specify the XPath or Xquery since these will be used generically for various XML documents without getting into a lot of details of the why… Just looking to see if I can do this with XDocument or if there is a better approach still not specifying the tag names in a XPath or XQuery unless it can be done dynamically without knowing how many levels deep the document may be.,

<?xml version="1.0"?>
<Root> 
<Customer>
<CustID>1</CustID>
<Name>Smith, Joe</Name>
<CustomerTransDetail>
<CustID>1</CustID>
<CustTransID>1</CustTransID>
<Note>NA</Note>
</CustomerTransDetail>
<CustomerTransDetail>
<CustID>1</CustID>
<CustTransID>2</CustTransID>
<Note>N/A</Note>
</CustomerTransDetail>
</Customer>
<Customer>
<CustID>2</CustID>
<Name>Smith, Jane</Name>
<CustomerTransDetail>
<CustID>2</CustID>
<CustTransID>1</CustTransID>
<Note>N/A</Note>
</CustomerTransDetail>
<CustomerTransDetail>
<CustID>2</CustID>
<CustTransID>2</CustTransID>
<Note>N/A</Note>
</CustomerTransDetail>
</Customer>
</Root>

I need to know when I hit the last child of the parent so I can do something. So every time I hit Name node or Note node for example I can do something.

  • 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-07T08:06:17+00:00Added an answer on June 7, 2026 at 8:06 am

    I’m still a little unclear as to what you’re attempting to do, but hopefully this can point you in the right direction.

    You’re using XDocument, so I’m assuming you’re in at least .NET 3.5. You can most likely use the Linq to XML extension methods to achieve what you want.

    XDocument doc = XDocument.Parse("xmlPath");
    foreach (XElement element in doc.Root.Descendants())
    {
        bool hasChildren = element.HasElements;
        bool hasSiblings = element.ElementsBeforeSelf().Any() || element.ElementsAfterSelf().Any();
        bool isLastChild = hasSiblings && !element.ElementsAfterSelf().Any();
    }
    

    Depending on what you need to do as you traverse the nodes, you may want to consider recursion as you walk the tree. That way you can determine as you go whether the element has siblings, is the last sibling, etc.

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

Sidebar

Related Questions

I am doing motion recognition of walking using openCV and C++ and I would
I'm looking to parse a xml file using Python and I was wondering if
While walking HTML5 DOM tree I want to determine whether each element is a
I am walking through a Visual FoxPro program trying to document the code in
I am using the Google Appengine remote shell, with Python. I am walking through
I am walking through a xml definition file and I have a DOMNodeList that
I'm using google overlays to draw my walking route in real time, I have
On a mac in python 2.7 when walking through directories using os.walk my script
I was walking view definitions in a database today using sqlalchemy 0.6 (using get_view_definition())
I've implemented a directory walking algorithm for the Windows Shell using IShellItem , IShellFolder

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.