LINQ to XML Code Review
Code Download
- Download Description:linq to xml download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:11 KB
Define the Person XML File
The below XML is stored in the People.xml file and is used to specify person data to be queried using LINQ to XML.
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person>
<id>1</id>
<firstname>Carl</firstname>
<lastname>Lewis</lastname>
<idrole>1</idrole>
</person>
<person>
<id>2</id>
<firstname>Tom</firstname>
<lastname>Gray</lastname>
<idrole>2</idrole>
</person>
<person>
<id>3</id>
<firstname>Mary</firstname>
<lastname>Grant</lastname>
<idrole>2</idrole>
</person>
<person>
<id>4</id>
<firstname>Fabio Claudio</firstname>
<lastname>Ferracchiati</lastname>
<idrole>1</idrole>
</person>
<role>
<id>1</id>
<roledescription>Manager</roledescription>
</role>
<role>
<id>2</id>
<roledescription>Developer</roledescription>
</role>
<salary>
<idperson id="1" year="2004" salaryyear="10000,0000" />
<idperson id="1" year="2005" salaryyear="15000,0000" />
</salary>
</people>
Retrieve Person Data using LINQ to XML
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.
class Program
{
static void GetPersonData()
{
XDocument xml = XDocument.Load(@"..\..\..\..\People.xml");
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.
static void GetPersonDataNoRootNode()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void GetPersonDataByAttributeValues()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void GetPersonDataWithJoin()
{
XDocument xml = XDocument.Load(@"..\..\..\..\People.xml");
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.
static void NavigateXmlTree()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void GetPersonDataBeforeSelfAfterSelf()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void GetPersonDataParent()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void XmlTransformation()
{
XElement xml = XElement.Load(@"..\..\..\..\People.xml");
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.
static void CreatePeopleXmlUsingXmlReaderXmlWriter()
{
XmlReader reader = XmlReader.Create(@"..\..\..\..\People.xml");
XDocument xml = XDocument.Load(reader);
Console.WriteLine(xml);
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.
static void Main(string[] args)
{
GetPersonData();
GetPersonDataNoRootNode();
GetPersonDataByAttributeValues();
GetPersonDataWithJoin();
NavigateXmlTree();
GetPersonDataBeforeSelfAfterSelf();
GetPersonDataParent();
XmlTransformation();
CreatePeopleXmlUsingXmlReaderXmlWriter();
Console.Read();
}
}