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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:58:41+00:00 2026-05-14T08:58:41+00:00

I have an XML doc like this: <Persons> <Person Id=10000 FullName=Jon Doe> <Status StatusID=1

  • 0

I have an XML doc like this:

<Persons>
 <Person Id="10000" FullName="Jon Doe">
  <Status StatusID="1" StatusDesc="Active"/>
      <Fields>
          <Field FieldId="1" Value="xxxx"/>
          <Field FieldId="2" Value="yyyy"/>
          <Field FieldId="2" Value="zzzz"/>
      </Fields>
 </Person>
 <Person Id="10001" FullName="John Smith">
  <Status StatusID="2" StatusDesc="New"/>
  <Fields>
      <Field FieldId="3" Value="aaaa"/>
      <Field FieldId="4" Value="bbbb"/>
     <Field FieldId="5" Value="ccccv"/>
  </Fields>
 </Person>
</Persons>

I want to write an XML query that returns the “Person” ID and all “Fields” elements.
I can get all “Fields” elements but not the “Person” ID.
The same applies when I need the “Status” element.

Any help will be appreciated.

  • 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-14T08:58:41+00:00Added an answer on May 14, 2026 at 8:58 am

    Try something like this:

    var result = XElement.Load("Example.xml")
      .Elements("Person")
      .Select(p => new {
       Id = p.Attribute("Id").Value,
       Fields = p.Descendants("Field").Select(f => new {
          Id = f.Attribute("FieldId").Value,
          Value = f.Attribute("Value").Value
         })
      });
    

    This will give you a sequence of anonymous types that look something like this:

    class Anonymous
    {
        public String Id { get; }
        public IEnumerable<AnonymousSubtype> Fields { get; }
    }
    
    class AnonymousSubtype
    {
       public String Id { get; }
       public String Value { get; }
    }
    

    The reason that I used the Descendants method to retrieve the fields is because the element I am first working with is the Person element. Since Elements only returns nodes that are direct children it would not work to retrieve the fields so I used Descendants instead.

    To enumerate the results you can do this:

    foreach (var person in result)
    {
        Console.WriteLine("Person Id: {0}", person.Id);
        foreach (var field in person.Fields)
        {
            Console.Write("  Field Id: {0}", field.Id);
            Console.WriteLine("  Field Value: {0}", field.Value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an XML doc like this: <books> <book>1110</book> <book>1111</book> <book>1112</book> <book>1113</book>
I have an XML doc with a structure like this: <Book> <Title title=Door Three/>
I have xml that looks like this: <Doc> <Thing> <ID type=One>Fred</ID> </Thing> <Thing> <ID>Bill</ID>
So I have a simple XML Doc like this: <Xml> <Node1></Node1> <Node2></Node2> ... <Node10></Node10>
I have .xml like this: <Type> <Connections> <Conn ServerName=serv1 DataBase=Persons User=admin Pass=123/> <Conn ServerName=serv2
If I have a document like this: <!-- in doc.xml --> <a> <b> greetings?
I have an xml document like this... this xml doc provides content for webpages
I have an XML doc that looks like this. <Results> <Name>Lab Asst1 </Name> <Subject>
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which

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.