I'm just back from TechEd Europe where I did a few sessions around LINQ to XML and LINQ to Entities along with taking a minor part in some panel discussions. Big thanks to all the people who came along to the sessions - hope you enjoyed it and thanks a lot for completing evaluations - much appreciated.
One of the requests that I got a few times was that I share the various bits of code that I used at the LINQ to XML session and so I thought that I'd do that here.
The session was in a few different sections as outlined below (this might prove to be a long post :-)).
1 - Introduction
In the introduction, I was just trying to make the point that the existing XML API's are not always the easiest to work with. The idea is that given a document that looks something like this;
If I want to use an XML API (rather than an XSL Transform) to programmatically query this and perhaps pull out all the entries where the country attribute is equal to "UK" and transform the results into a document that looks a little like this;
then doing that with the existing API's isn't that much fun and it's a lot more fun with LINQ to XML.
I've included the code for this here.
Creating XML - XElement and XAttribute
When it comes to creating XML, I showed how we can use XElement and XAttribute to create XML in a very natural way. The steps that (I think :-)) I went through are something like this;
- Create a single root element with XElement.
- Create a child and add it to the tree by calling root.Add().
- Create an attribute and add it to the tree in the same way, showing that Add() takes Object.
- Create 3 children and add them all to the tree.
- Show that Add() can take any list of Object and so we can add all our children and attributes in one line of code.
- Show that Add() can also take IEnumerable<T> (not for every "T"!) and so we can add (e.g.) List<XElement>.
- Show that the constructor of XElement can also do the same as Add() so we don't necessarily have to call Add().
- Show the Numbers example.
- Show the Processes example.
- Show the XAML example.
I've put the code for this up here.
Creating XML - XDocument & Friends
XElement and XAttribute do an awful lot of work for you but there's other classes within the API and we can demonstrate that with code that I've included here.
Namespaces in XML are always a pain to deal with from API's (namespace managers, anyone?) but LINQ to XML makes it pretty painless - I've included the code sample here with all the bits in it.
In terms of querying, I ran through 3 different queries. One pulled out id attributes from some <customer> elements. Another that looked for some <order> elements that had a particular datetime value and a final one that did some grouping and ordering. All of those samples are included in this example here.
For modifications, we looked a little at Add, AddBeforeSelf, AddAfterSelf, RemoveNodes, RemoveAttributes, RemoveAll, Remove, ReplaceWith. We used this code here (or something like it)
Visual Basic and XML :-)
VB9 is great when it comes to XML. We showed a little example including creating, querying and namespaces. Solution file is here.
Other Topics (Schema, Annotations, Working with Large Documents)
In terms of the large documents stuff, I wrote something about that here (with code).
In terms of the schema topic, solution file is here.
In terms of the annotations topic, solution file is here.
Sat, Nov 10 2007 4:12 AM