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 ></NextItem>
<PreviousItem id="9">Stand2 ></PreviousItem>
</Sequences>
</Sample>
<Sample name="Hill" id="1" batch="1">
<Sequences>
<NextItem id="1">Stand1 ></NextItem>
<NextItem id="2">Stand3 ></NextItem>
<PreviousItem id="3">Stand4 ></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)