Friday, February 5, 2010

XML with LINQ

Parent child Relationship:
public static object GetMLAMemberDetailsByConstituency(string Id) {
XDocument Xdoc = XDocument.Load("Root.Data/DataXML/MLAXML.xml");
var settingElement = from us in Xdoc.Elements("root").Elements ("State").Elements("PC").Elements("Constituency").Elements("PM").Elements("PT")
where us.Parent.Parent.Attribute ("ConstituencyId").Value == Id
select new
{
FirstName = us.Parent.Attribute ("FirstName").Value,
LastName = us.Parent.Attribute("LastName").Value,
State = us.Parent.Parent.Parent.Parent.Attribute("StateName").Value,
PC = us.Parent.Parent.Parent.Attribute("PCName").Value,
constituency = us.Parent.Parent.Attribute("ConstituencyName").Value,
ConstituencyId = us.Parent.Parent.Attribute("ConstituencyId").Value,
PartyName = us.Attribute("PartyName").Value,
};
return settingElement.ToList();
}
More Details:
http://www.aspnettutorials.com/tutorials/xml/linq-to-xml-adding-cs.aspx