LINQ to XML Code Review
Retrieve Person Data using LINQ to XML
Code Review
- LINQ to XML - Part 1: Define the Person XML File
- LINQ to XML - Part 2: Retrieve Person Data using LINQ to XML
Code Walkthrough
Load the People.xml data into a XDocument object and return all person elements where the id element has a value of 1, starting from the root node.
var query = from p in xml.Elements("people").Elements("person") where (int)p.Element("id") == 1 select p;
foreach (var row in query) { Console.WriteLine("Person: {0} {1}",row.Element("firstname").Value,row.Element("lastname").Value); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and return all person elements where the id element has a value of 1, ignoring the parent node.
var query = from p in xml.Elements("person") where (int)p.Element("id") == 1 select p;
foreach (var row in query) { Console.WriteLine("Person: {0} {1}",row.Element("firstname"),row.Element("lastname")); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and return all salary elements where the year element has a value of 2004.
var query = from s in xml.Elements("salary").Elements("idperson") where (int)s.Attribute("year") == 2004 select s;
foreach (var row in query) { Console.WriteLine("Amount: {0}", row.Attribute("salaryyear")); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and navigate through all nested elements joining all person elements to idperson elements based on person ID element equal to idperson ID attribute.
var query = from p in xml.Descendants("person") join s in xml.Descendants("idperson") on (int)p.Element("id") equals (int)s.Attribute("id") select new { FirstName = p.Element("firstname").Value, LastName = p.Element("lastname").Value, Amount = s.Attribute("salaryyear").Value };
foreach (var row in query) { Console.WriteLine("Person: {0} {1}, Salary: {2}",row.FirstName, row.LastName, row.Amount); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and return all the parent (ancestor) elements for the first firstname element.
var query = xml.Descendants("firstname").First();
foreach (var tag in query.Ancestors()) { Console.WriteLine(tag.Name); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and return all elements before and after the first firstname element.
XElement firstname = xml.Descendants("firstname").First();
Console.WriteLine("Before <firstname>"); foreach (var tag in firstname.ElementsBeforeSelf()) { Console.WriteLine(tag.Name); } Console.WriteLine(); Console.WriteLine("After <firstname>");
foreach (var tag in firstname.ElementsAfterSelf()) { Console.WriteLine(tag.Name); } Console.WriteLine(); }
Load the People.xml data into a XDocument object and return the entire parent element containing the first firstname element.
XElement firstname = xml.Descendants("firstname").First();
Console.WriteLine(firstname.Parent); Console.WriteLine(); }
Load the People.xml data into a XDocument object and create an html document containing all person data, with the corresponding role data, joining all person elements to role elements based on person ID role element equal to role ID element.
XElement html = new XElement("HTML", new XElement("BODY"), new XElement("TABLE", new XElement("TH", "TD"), new XElement("TH", "Full Name"), new XElement("TH", "Role"), from p in xml.Descendants("person") join r in xml.Descendants("role") on (int)p.Element("idrole") equals (int)r.Element("id") select new XElement("TR", new XElement("TD", p.Element("id").Value, new XElement("TD", p.Element("firstname").Value + " " + p.Element("lastname").Value, new XElement("TD", r.Element("roledescription").Value ))))));
html.Save(@"..\..\..\..\People.html"); Console.WriteLine("People.html saved."); Console.WriteLine(); }
Load the People.xml data into a XmlDocument object and append a new idperson element after the last idperson element in the salary element.
XElement idperson = xml.Descendants("idperson").Last();
idperson.Add(new XElement("idperson", new XAttribute("id", 1), new XAttribute("year", 2006), new XAttribute("salaryyear", "16000,0000")));
StringWriter sw = new StringWriter(); XmlWriter w = XmlWriter.Create(sw); xml.Save(w); w.Close(); Console.WriteLine(sw.ToString()); Console.WriteLine(); }
The static Main() method defines various LINQ functions to be applied against the XML data.
Console.Read(); } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!