Scott Hanselman

Clever: XML to Schema Inference Wizard for Visual Studio 2008

September 15, 2007 Comment on this post [4] Posted in LINQ | Microsoft
Sponsored By

VB9 support for XML is pretty sexy, as I've mentioned before. Specifically, the XML Literals may have me using VB more often, or, more likely, mixing VB9 and C# together. More on that soon.

Here's some C# LINQ to XML:

XDocument booksXML = XDocument.Load(Server.MapPath("books.xml"));        
var books = from book in 
                    booksXML.Descendants("{http://example.books.com}book")
                    select book.Element("{http://example.books.com}title").Value;
Response.Write(String.Format("Found {0} books!", books.Count()));

Here's the same code in VB:

Dim booksXML = XDocument.Load(Server.MapPath("books.xml"))
Dim books = From book In booksXML...<b:book> Select book.<b:title>.Value
Response.Write(String.Format("Found {0} books!", books.Count()))

What's significant is the intellisense for your XML, created via schema, within your VB code.

To make it clearer:

VB9LINQXML

I've just pressed "." after typing "book.<" and I've got intellisense for my XSD-described XML document. Slick.

However, this assumes that you actually HAVE a schema, either having written one, or inferred one. I would hazard to guess that many (most?) projects haven't created an XSD. Heck, I see a lot of XML with no namespace at all! Without an XSD, Visual Basic can't give you this experience.

The teams have just snuck a new Project Item Template up onto Microsoft Download Center for use in Orcas. It's the XML to Schema Inference Wizard. Here's the deal.

You've got some XML somewhere without a schema and you're working on it in a project. You want intellisense for XML in VB9, so you right click on your project and select Add | New Item and see this dialog. (NOTE: you might need to SCROLL DOWN to see it!)

Add New Item - CUsersScottDesktopASP.NET OrcasNew CodeChapter 9VBListing9-10q_app

See the new item, Xml To Schema? Name your new schema and select Add and you'll see this dialog:

Infer XML Schema from XML documents

What's cool here is that you can Add from File, Add from Web or even Paste in an example XML document. You can add as many as you like, then click OK.

The new schemas will be inferred using the XML inference APIs (you could have done this one by one with xsd.exe from the command line, but that can be tedious, and also command-lines tend to squash Joe Public's fun if they are required.) and added to your project.

Now, in your VB code, type "Imports <xmlns:yourprefix=" and you'll get Intellisense with all the namespaces that you want to make available for your LINQ to XML expressions.

ImportsXMLNamespace

And that's it. At this point, you've associated the fully qualified namespace with whatever prefix you chose, and you'll have Intellisense for all your XML within VB9. Note that the inference isn't perfect, as we're "gleaning" a lot, so the more representative of reality your instance document, the better. You're welcome to hand-edit them also if they get to 80% and you want to take the XSD all the way.

Be sure to check out Beth Massi's Blog's LINQ Category for more detail on VB9 and LINQ.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
September 15, 2007 11:23
"or, more likely, mixing VB9 and C# together. More on that soon" - Dude you can't leave me hanging like that!!

See my blog post (http://rorybecker.blogspot.com/2006/09/i-want-multi-language-projects-in.html)

...and an earlier back and forth on Channel9 (http://channel9.msdn.com/ShowPost.aspx?PostID=6408)
September 15, 2007 11:57
Ya, I've got some ILMerge ideas.
September 15, 2007 19:17
Thats very cool.
I know it's nitpicking but.. "Note that the interference isn't perfect" I think you meant inference ;-)
Ian
September 15, 2007 20:52
Doh!

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.