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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:56:07+00:00 2026-06-01T08:56:07+00:00

I have an xml from which I am trying to extract some information through

  • 0

I have an xml from which I am trying to extract some information through LINQ query.
The format of the xml file looks like the following:

<TopNode>
  <Sample name="Tom" id="0" batch="1">
    <Sequences>
      <NextItem id="10">Stand1 &gt;</NextItem>
      <PreviousItem id="9">Stand2 &gt;</PreviousItem>
    </Sequences>
  </Sample>
  <Sample name="Hill" id="1" batch="1">
    <Sequences>
      <NextItem id="1">Stand1 &gt;</NextItem>
      <NextItem id="2">Stand3 &gt;</NextItem>
      <PreviousItem id="3">Stand4 &gt;</PreviousItem>
    </Sequences>
  </Sample>
.
.
.
</TopNode>

I have to get a list of attributes of both NextItem and PreviousItem from the query. For this, I defined a class to get my results.

public class Extract
{
public string name 
{
get;
set;
}
public int _id
{
get;
set;
}
}

LINQ Query (foreach Sample node, get the PreviousItem and NextItem content in class object):

var EnumerableContent = from item in XElement.Load("file.xml").Elements("NextItem")
              select ???...

The problem is how do I pass the data to the class objects here in the above query for each Sample node.
.
Secondly, the above will only get me NextItem nodes. How do I write the query for both NextItem and PreviousItem?

EDIT
On the whole, I have to get an IEnumerable for each for each of the Sequence nodes, and then return the overall IEnumerable from thie query.

EDIT2
Assigning key here is giving me this error. Query below

var mapping = XDocument.Load("file.xml")
                          .Descendants("Sequences")
                          .Select(n => n.Descendants("PreviousItem")
                              .Union(n.Descendants("NextItem"))
                              .Select(n1 => new Extract { _id = (int)n1.Attribute("id"), Name = n1.Value })
                              ).ToDictionary<IEnumberable<Menu>, int>(key => key._id);//and further converting this dictionary to sorted dictionary (sorted accordign to keys)
  • 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-01T08:56:09+00:00Added an answer on June 1, 2026 at 8:56 am
    var xDoc = XDocument.Load(....);
    var result = xDoc.Descendants("NextItem")
      .Union(xDoc.Descendants("PreviousItem"))
      .Select(n => new {ID = n.Attribute("id").Value, Name =n.Name, Value =n.Value });
    

    –EDIT–

    var result = XDocument.Load(....)
                .Descendants("Sequences")
                .Select(n=> n.Descendants("NextItem")
                     .Union(n.Descendants("PreviousItem"))
                     .Select(n2 => new { ID = n2.Attribute("id").Value, Name = n2.Name, Value = n2.Value })
                );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file(from federal government's data.gov) which I'm trying to read with
I have a windows service which is trying to access an xml file from
I have a class which is serialized to and from an XML file when
I have a function TRANSLATE which reads value from the xml file based on
I am trying to extract some data from a website using a LINQ statement,
Greetings I'm trying to get an xml file from a website which requires me
I have the following XML from which I am trying to generate HTML files.
I've got some code which draws data from an xml file but it seems
I'm trying to extract some XML from a UIWebView , and not having much
Hey guys. I'm trying to parse xml file in order to extract some data

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.