Out of the Angle Brackets
Announcing the LINQ to XSD Preview
Ever since PDC 2005, when XLinq was unleashed (now called LINQ to XML), we have been receiving questions about the possibility of a typed XML programming approach in the LINQ world. Not surprisingly, we have been pondering about this problem indeed. This was also mentioned in the XLinq overview. (Please, also see the “Typed XML programmer” series of blog posts.) For some time now we have been focusing incubation efforts on typed XML programming for LINQ. One overall approach is to engage in a mapping of XML types to ‘plain’ object types, that is, classes with ‘fields that maintain state’ and with support for XML serialization and de-serialization. While this approach would effectively reduce typed XML programming to ‘LINQ to objects’ + de-/serialization, it potentially leads to a lossy and complicated XML programming model. Consequently, our efforts have focused on a more natural and faithful approach which builds on top of LINQ to XML.
Today we are happy to announce the LINQ to XSD Preview Alpha 0.1:
We'd like to encourage everyone with a stake in XML programming and ‘schema-first’ applications to take a look at LINQ to XSD and send your observations, questions and suggestions this way; we'll be eagerly waiting for feedback! The whole point of this incubation project is to investigate options for the future of typed XML programming. We are still very early in the design process. So your feedback is critical and can help shape the design of the project as we are going forward.
This is pre-release software; it is provided “as-is” – we’re crucially interested in getting feedback from customers but we can’t make any guarantee with regard to supporting or maintaining this software or releasing future versions on any specific timeline. In particular, while the underlying LINQ to XML technology is expected for Orcas, no such commitment exists for LINQ to XSD.
What is LINQ to XSD?
LINQ to XSD provides .NET developers with support for typed XML programming on top of LINQ to XML. While the LINQ to XML programmer operates on generic XML trees, the LINQ to XSD programmer operates on typed XML trees -- instances of .NET types that model the XML types of a specific XML schema (XSD). To get an idea, consider the following C#3.0 fragment for a LINQ to XML query that computes the total over the items in a XML tree for a purchase order:
(from item in purchaseOrder.Elements("Item")
Using LINQ to XSD, the query is written in a much clearer and type-safe way:
(from item in purchaseOrder.Item
select item.Price * item.Quantity
LINQ to XSD can be used whenever you have an XML schema available, or you are willing to infer a schema from the XML data at hand. LINQ to XSD is integrated into Visual Studio; so you just tag an XML schema as an ‘LINQ to XSD schema’, build your project, and the automatically derived object model is then part of your solution -- just as if XML schemas were .NET types.
The derived object model enforces various validation constraints imposed by the underlying XML schema -- quite similar to other technologies for so-called X/O mapping or XML data binding. Two important aspects of LINQ to XSD are its integration into the LINQ family of technologies and its foundation on ‘XML objects’ as opposed to ‘plain objects’. That is, the derived object models provide XML semantics in terms of XML fidelity and programming idioms. To this end, the derived object models define .NET types (classes) that are essentially views on generic LINQ to XML trees. Typed member access and other typed axes can be used in most cases, while untyped tree access is still available, when necessary.
What’s in the release?
LINQ to XSD-related pointers
We really look forward receiving your feedback.
Data Programmability / XML team
Georges question is something that probably many people have in mind
We had some articles on this topic here in xml team blog, but of course it will be interesting to get back to this subject ;ppp
For sure l2xsd have two advantages:
- generated classes inherit from XElement ; so they are interopable with rest of xlinq infrastructure. What doest that mean? Generated classes can be treated generlicly as untyped xml
- generated classes are doing some sort of xsd-based constraints checks when making modifications. I had not looked at generated code (performance overhead ? can those constrains checks be turned off & on by some document/element scope ?) but it seems that there is something like this
anything more ?
hmm and besides there is some quite important subject
XML INDEXING for querying ;p
As far as I know in System.Xml & in xlinq there is no xml indexing at all
As such indexing needs some way to specify what subelements/attributes should be indexed it is nice to get back to this topic while working with xsd. Maybe some sort of “indexthis” annotations in xsd ?
Some might say “indexing is for databases”. Yes it is. But now ram is big enough to work with so called “in memory” databases. So instead of using sql server xml indexing we might have linq based in memory xml database. We need two things:
- indexing. As mentioned above
- disk written change log. This could be embedded in generated setters methods
Of course some transaction support and locking will be nice, but those two are quite a lot ;p
And Of course same for linq2objects. We need indexes supporting collections with index aware IQueryable implementations. It is A MUST
LINQ to XSD has been released in alpha form by the XML team, and they are requesting feedback. You can...
Hey I like the way how people end up saying l2xsd instead of LINQ to XSD.
I saw some interesting questions.
Re: "differences xsd.exe vs. l2xsd". I should be careful in comparing actual platform technology (xsd.exe) with an incubation project (l2xsd). So let's try to focus on high-level conceptual and hopefully timeless issues -- also understanding that l2xsd is a moving target (and your feedback is critically wanted). Please also check out the “Typed XML Programmer” series on the team blog, as it is going into some of this in quite some detail; see index here http://blogs.msdn.com/ralflammel/archive/2006/11/13/table-of-contents-for-typed-xml-programmer-series.aspx and then there is also the long version : see the booklet “Revealing the X/O impedance mismatch” on my website. So the exec summary, IMHO, goes here: with a de-/serialization-based technology (such as xsd.exe) you escape from XML semantics (fidelity, query model, tree representation, …) to simple stateful objects. With a native XML technology like LINQ to XSD you get a richer and cleaner programming model because you stay within the XML semantics. You are not going to be surprised by round-tripping issues resulting from shredding issues in turn. You can walk your parent, siblings, etc. You do not need to serialize (materialize!) an object graph that is then orphaned (offline) from the XML underneath. And so on …
These are deep semantical and conceptual differences I wouldn’t want to glance over. Now, does a business programmer bother? This depends on the complexity of the XML and schemas, on the requirements for XML fidelity, on the appreciation of the LINQ to XML query model, on the overall question whether schema-first is relevant enough for us to work on a technology for it. I am just writing up the XML 2006 paper on l2xsd which I will post here within weeks – I make sure to cover this sort of question in some depth. As to performance, this topic also deserves separate treatise. One way to think of it is this: suppose we have a LINQ to XML tree in memory, then what is the cost of creating the typed view in a demand-driven way? Ideally, *those* costs are pretty small. There is so many tricks you can play, but not necessarily in an incubation project that we use to gather all sorts of feedback, primarily related to the programming model.
BTW, yes, l2xsd classes could inherit from XElement but they rather *wrap* XElement since the pragmatics work out better that way. In particular, you don’t need to know the precise l2xsd type when the XML trees gets created but only once you want to view through typed glasses.
And yes ,l2xsd classes do check for simple-type restrictions and other constraints (see the Readme and the docs for limitations and open issues), but this is nothing that a serialization-based approach would be intrinsically incapable of. However, it is worth noting that this constraint checking business gets us intrinsically away from a field-based model. So we need ADTs really. This is what l2xsd takes to the next level by saying not even the state of the XML objects resides in the typed view, but the typed access properties directly access the ultimate XML state of LINQ to XML trees.
Hi, do you guys support recursive element definitions in XSD? If so, how? Is it documented anywhere?
I've found it documented all over as supposedly possible in XSD, with numerous examples, but the .NET 2.0 XML parser barfs and claims the element is "not declared". I can do it with something resembling a forward declaration (found that in the sqlxml docs), but then of course the "children" aren't validated at all.
if you mean (mutually or directly) recursive element *declarations*, yep, of course, this is covered by XSD and so it is by LINQ to XSD. Not sure that I understand what XML APIs you are having problems with. Please send me a repro, if you like, email@example.com. I should be able to forward it as appropriate.
I downloaded the LINQ to XSD alpha preview. After reading the disclaimer multiple times (it seems Microsoft...
DonXML says (referring to PLinq): "Overall, this stuff looks very cool. And it seems that LINQ is going to be a big part of how they plan to implement it, giving me even more of a reason to get going on LINQ before all the cool kids realize that WPF, WCF and WF are yesterday's news, and LINQ is where it is at ;)"
The same applies to LINQ for XSD. See http://oakleafblog.blogspot.com/2006/12/reduce-objectxml-impedance-mismatch.html.
In a comment on “ More Haskell in Java 7 or 8? ”, I get this question “How does your `Retroactive Interface
I had a lot of time to think about Elliotte Harold's call for XML predictions on the way home from Redmond
I haven't written about LINQ to XSD yet on this blog. So, here goes... LINQ to XML has been revealed
Architecture More competition! No, I am very glad to see my good friend and Architect Harry start a series
Como no podía ser de otra forma, y después de hablar de las bases de LINQ y de LINQ To SQL , desde el