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:
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 })
);
I'm not sure I understood the question, but you can easily take element/attribute value inside LINQ to XML select
statement:
var EnumerableContent = from item in XElement.Load("file.xml").Elements("NextItem")
select new Extract() { name = item.Value, _id = (int)item.Attribute("id") }
hi i think this is what you expect.
List<XElement> lstXElements = new List<XElement>();
lstXElements.AddRange(GetDescendants("NextItem"));
lstXElements.AddRange(GetDescendants("PreviousItem"));
List<Extract> lstExtract = new List<Extract>();
foreach (XElement objElement in lstXElements)
{
Extract objExtract = new Extract();
objExtract._id = Convert.ToInt32(objElement.Attribute("id").Value);
objExtract.name = (objElement.Name).LocalName;
lstExtract.Add(objExtract);
}
List<XElement> GetDescendants(string strDescentName)
{
return ((XDocument.Load(Server.MapPath("XMLFile1.xml"))
.Descendants(strDescentName))
).ToList<XElement>();
}