Mike Taulty's Blog
Bits and Bytes from Microsoft UK
TechEd Europe - LINQ to XML Session Code

Blogs

Mike Taulty's Blog

Elsewhere

Archives

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;

image

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;

<ukCustomers>

  <ukCustomer>

    <name>Some Name</name>

    <title>Some Title</title>

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;

  1. Create a single root element with XElement.
  2. Create a child and add it to the tree by calling root.Add().
  3. Create an attribute and add it to the tree in the same way, showing that Add() takes Object.
  4. Create 3 children and add them all to the tree.
  5. Show that Add() can take any list of Object and so we can add all our children and attributes in one line of code.
  6. Show that Add() can also take IEnumerable<T> (not for every "T"!) and so we can add (e.g.) List<XElement>.
  7. Show that the constructor of XElement can also do the same as Add() so we don't necessarily have to call Add().
  8. Show the Numbers example.
  9. Show the Processes example.
  10. 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

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.

Querying XML

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.

Modifying XML

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.

Enjoy! :-)


Posted Sat, Nov 10 2007 4:12 AM by mtaulty

Comments

Jason Haley wrote Interesting Finds: November 10, 2007
on Sat, Nov 10 2007 8:55 AM
simply24 » Blog Archive » TechEd Europe - LINQ to XML Session Code wrote simply24 &raquo; Blog Archive &raquo; TechEd Europe - LINQ to XML Session Code
on Sat, Nov 10 2007 8:59 AM
Christopher Steen wrote Link Listing - November 10, 2007
on Sat, Nov 10 2007 9:17 PM
ASP.NET  The REST-Like Aspect Of ASP.NET MVC [Via: Haacked ] WPF  Routed Event Viewer [Via: Karl Shifflett...
Christopher Steen wrote Link Listing - November 10, 2007
on Sat, Nov 10 2007 9:17 PM
Link Listing - November 10, 2007
KHIMICH wrote KHIMICH
on Sun, Nov 11 2007 11:04 AM
PingBack from http://khimich.com/
Feed Search Engine - All Fresh Articles And News Are Here wrote Feed Search Engine - All Fresh Articles And News Are Here
on Sat, Nov 24 2007 6:19 PM
NBAVids - Basketball Rocks » TechEd Europe - LINQ to XML Session Code wrote NBAVids - Basketball Rocks &raquo; TechEd Europe - LINQ to XML Session Code
on Wed, Nov 28 2007 12:31 AM