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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:03:01+00:00 2026-06-17T05:03:01+00:00

If I have a XML-document like this <Doc> <File> <ObjectID></ObjectID> </File> </Doc> And it

  • 0

If I have a XML-document like this

<Doc>
   <File>
      <ObjectID></ObjectID>
   </File>
</Doc>

And it sometimes can look like this

<Doc>
   <Name />
   <File>
      <ObjectID></ObjectID>
   </File>
</Doc>

How can I get the File-elements?
They are not always on the same level, sometimes they can be on the root-level
There could also be times, when it is placed multiple times in the Document (but then they are at the same level)

UPDATE

The <File>-element can also have <File> elements below, which should not be parsed like this

<Doc>
   <Name />
   <File>
      <ObjectID></ObjectID>
      <RelatedTo>
         <File></File>
      </RelatedTo>
   </File>
</Doc>

In which case the result should be

<File>
   <ObjectID></ObjectID>
   <RelatedTo>
      <File></File>
   </RelatedTo>
</File>
  • 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-17T05:03:02+00:00Added an answer on June 17, 2026 at 5:03 am

    With s being the string which contains your XML code:

    var document = XDocument.Parse(s);
    var fileElements = document.Descendants("File").
                           ToArray();
    

    Note that you won’t get one single element but an array. In case there’s only one in your document, then the array will have just one element.

    The above approach will work even if the File elements are children of other elements, but I’m not sure what you meant by them being at “root level”. Isn’t that the case that you brought us?

    EDIT: To adapt it to the author’s edit:

    var elementName = "File";
    var firstFileElement = document.Descendants(elementName).
        First();
    var fileElements = firstFileElement.Parent.Elements(elementName).
        ToArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>0.01</Value> <Result>Pass</Result> </Test>
I have an XML file formatted like this: <?xml version=1.0 encoding=utf-8?> <Snippets> <Snippet name=abc>
I have a xml document like this : <Node1 attrib1=abc> <node1_1> <node1_1_1 attrib2 =
Hi I have an xml document like this <root> <cert id=1> </cert> <cert id=2>
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
I have an XML document that matches our site navigation something like this: <page
So I have a xml document loaded in JavaScript variable. Xml looks like this:
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have an XML file in the following format: <doc> <id name=X> <type name=A>

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.