Linq to XSD: strong-typed XML data models!

I'm very pleased to see the direction the XML team took with the new Linq to XSD preview. Basically, it's xsd.exe, but generating classes on top of Linq to XML (or XLinq, XElement and friends). This is pretty cool, because it gives you all the power and productivity of a strong-typed object model, while still allowing for the freedom of manipulating the underlying untyped XML.

The crucial point there is that the typed model is pretty much an adapter on top of untyped XML, therefore both will always be in sync. The semantics for the insertion and appending of content data to the underlying untyped model is sound and I'm sure it will be even better by the time it ships.

You can now transform the following:

 

(from item in purchaseOrder.Elements("Item")

select (double)item.Element("Price")

* (int)item.Element("Quantity")

).Sum();

 

into:

 

(from item in purchaseOrder.Item

select item.Price * item.Quantity

).Sum();

 

The code generation is done with an MSBuild task, so you can always be sure that the XSD and its object model adapters are in sync, and your code always complies with it.

One question that remains to be seen is how/if this plays with the XStreamElement support. I guess they are orthogonal, but haven't checked.

Congrats to the team for a step in one very useful direction!

No Comments