LINQ to XSD - Typed XML programming with LINQ
I haven't written about LINQ to XSD yet on this blog. So, here goes...
LINQ to XML has been revealed with the first versions of LINQ. It was known as XLinq at that time. LINQ to XML allows querying and creating XML trees and documents. The limitation is that it works on generic XML. You deal with elements and attributes, but not in a strongly-typed way.
LINQ to XSD has been created to improve this. It allows typed XML programming on top of LINQ to XML.
LINQ to XSD, as you would expect, requires an XML Schema (XSD). This schema is used to generate an object model that can then be used to manipulate XML data through LINQ, while enforcing types and the various validation constraints specified in the XML schema.
To show you how LINQ to XSD makes the query syntax much safer and cleaner, let's look at a simple example.
Here is a LINQ to XML query:
from item in purchaseOrder.Elements("Item")
select (double)item.Element("Price") * (int)item.Element("Quantity")
Here is the same query as above, but written using LINQ to XSD:
from item in purchaseOrder.Item
select item.Price * item.Quantity
LINQ to XSD's benefits compared to LINQ to XML include:
- Improved readability
- Type-checking at compile time
- IntelliSense support (completion)
You can get the details on the announcement post by Microsoft's XML team.
As Steve points out, Ralf Lämmel - who is working on LINQ to XSD - has papers on the subject if you want to learn more.
Cross-posted from http://linqinaction.net