Introducing LINQ with XML and Databases - LINQ to XML
(Page 3 of 4 )
Now let's take a look at LINQ to XML. LINQ to XML gives us a new way to represent and work with XML. Let's examine this through example. Suppose we want to represent a group of people. For each person, we want to represent a name and an age. So, an XML representation of all of this data might look like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<people>
<person>
<name>Bob</name>
<age>35</age>
</person>
<person>
<name>Henry</name>
<age>43</age>
</person>
<person>
<name>Joe</name>
<age>22</age>
</person>
<person>
<name>Chuck</name>
<age>29</age>
</person>
</people>
In order to demonstrate the new API provided by LINQ to XML, let's actually build the above XML document first. You'll need to import the System.Xml.Linq namespace to get started. This namespace provides a variety of classes, of which we'll look at a few. The first is XDocument, which represents an XML document. Our entire document above can be contained within an XDocument class. The second is XDeclaration, which can contain the first line of our document – the XML declaration. The third is XElement, which represents an element. The body of our document can be represented by nesting XElement objects.
When creating, for example, an XElement object, we're able to pass in other child elements as objects. For example, here, we create a single person:
XElement bob = new XElement("person",
new XElement("name", "Bob"),
new XElement("age", "35")
);
Dim bob As New XElement("person", _
New XElement("name", "Bob"), _
New XElement("age", "35") _
)
If we print out the object, the proper XML is outputted:
Console.WriteLine(bob);
<person>
<name>Bob</name>
<age>35</age>
</person>
As you can see, building XML using the API is pretty straightforward. Let's now build the entire XML document:
XDocument document = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("people",
new XElement("person",
new XElement("name", "Bob"),
new XElement("age", "35")),
new XElement("person",
new XElement("name", "Henry"),
new XElement("age", "43")),
new XElement("person",
new XElement("name", "Joe"),
new XElement("age", "22")),
new XElement("person",
new XElement("name", "Chuck"),
new XElement("age", "29"))
)
);
Dim document As New XDocument( _
New XDeclaration("1.0", "utf-8", "yes"), _
New XElement("people", _
New XElement("person", _
New XElement("name", "Bob"), _
New XElement("age", "35")), _
New XElement("person", _
New XElement("name", "Henry"), _
New XElement("age", "43")), _
New XElement("person", _
New XElement("name", "Joe"), _
New XElement("age", "22")), _
New XElement("person", _
New XElement("name", "Chuck"), _
New XElement("age", "29")) _
) _
)
The result is actually quite readable and manageable, which is, of course, a very good thing. If we want to, we can save our document for future use:
document.Save("people.xml");
We can now query our document using a combination of the query syntax and the classes that we just went over. Let's retrieve everyone over the age of thirty:
var overThirty = from p in document.Descendants("person")
where (int)p.Element("age") > 30
select new
{
Name = (string)p.Element("name"),
Age = (int)p.Element("age")
};
Dim overThirty = From p In document.Descendants("person") _
Where CInt(p.Element("age")) > 30 _
Select New With { _
.Name = CStr(p.Element("name")), _
.Age = CInt(p.Element("age")) _
}
Unfortunately, we do have to cast each element to the proper type.
We're also able to go further and create a new XML document by querying an old one. For example, suppose wanted to create an entirely new XML document based on the people over the age of thirty. To do this, we just have to drop a query where we want the elements to go:
XDocument overThirtyDocument = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("people",
from p in document.Descendants("person")
where (int)p.Element("age") > 30
select p
)
);
Dim overThirtyDocument As New XDocument( _
New XDeclaration("1.0", "utf-8", "yes"), _
New XElement("people", _
From p In document.Descendants("person") _
Where CInt(p.Element("age")) > 30 _
Select p _
) _
)
Next: XML Literals in Visual Basic >>
More .NET Articles
More By Peyton McCullough