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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:46:32+00:00 2026-05-11T20:46:32+00:00

I have an xml that I would like to get all of its elements.

  • 0

I have an xml that I would like to get all of its elements. I tried getting those elements by Descendants() or DescendantNodes(), but both of them returned me repeated nodes

For example, here is my xml:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstElement xsi:type="myType">
    <SecondElement>A</SecondElement> 
  </FirstElement>
</Root>

and when I use this snippet:

 XElement Elements = XElement.Parse(XML);
 IEnumerable<XElement> xElement = Elements.Descendants();
 IEnumerable<XNode> xNodes = Elements.DescendantNodes();
 foreach (XNode node in xNodes )
 {
     stringBuilder.Append(node);
 }

it gives me two nodes but repeating the <SecondElement>. I know Descendants call its children, and children of a child all the time, but is there any other way to avoid it?

Then, this is the content of my stringBuilder:

<FirstElement xsi:type="myType"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SecondElement>A</SecondElement> 
</FirstElement>

<SecondElement>A</SecondElement>
  • 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-11T20:46:32+00:00Added an answer on May 11, 2026 at 8:46 pm

    Well do you actually want all the descendants or just the top-level elements? If you only want the top level ones, then use the Elements() method – that returns all the elements directly under the current node.

    The problem isn’t that nodes are being repeated – it’s that the higher-level nodes include the lower level nodes. So the higher-level node is being returned, then the lower-level one, and you’re writing out the whole of both of those nodes, which means you’re writing out the lower-level node twice.

    If you just write out, say, the name of the node you’re looking at, you won’t see a problem. But you haven’t said what you’re really trying to do, so I don’t know if that helps…

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

Sidebar

Related Questions

I have an ActiveRecord model that I would like to convert to xml, but
I have an XML file that I would like to map some attributes of
I have an existing XML file that I would like to append without changing
I have a string that represents a non indented XML that I would like
this is for an Android application, I have an XML file that I would
Me again... I have an XML File that contains different categories which I would
I have XML that looks something like this: <Root xmlns=http://widgetspecA.com/ns> ...any... <WidgetBox> <A/> <B/>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have XML that looks like this: <ROW ref=0005631 type=04 line=1 value=Australia/> <ROW ref=0005631
So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id

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.