To continue with my topic on things you can do in VB9 that you cannot (yet) do in any other .NET language, I wanted to write a bit about XML Schema Support and intellisense support for XML properties in VB9.
First, I highly recommend reading Eric Meijer and Brian Beckman’s research paper XLINQ: XML Programming Refactored (The Return Of The Monoids), which provides background as to the motivation and implementation of XLINQ technology. The Monads referred to in the paper remind us that the constructs being integrated into the .NET framework are enabling a more functional programming paradigm. This and the concurrent development of the dynamic language runtime promise to further expand the capabilities of the .NET platform to languages and programming approaches so that any programmer with any approach can make use of the platform in a fashion they are comfortable with.
The LINQ project, as many of you are aware, brings to all .NET languages capabilities languages like LISP and Scheme have long enjoyed, based on the notion of the Lambda-function. There are many examples for LINQ available so I will not bore you with that topic, however, what I do think is very interesting are the extensions to LINQ which support XML (XLINQ) as well as a number of other VB9-specific capabilities such as XML Literals that make working with and programming against XML infinitely easier.
This blog post explores the way in which Visual Studio 2008 and VB9 work together to enable programmers working with XML to:
1. Infer a schema based on a XML file itself (useful in cases where you have the XML file and not the schema);
2. Use the inferred schema to hook up Visual Studio’s intellisense capabilities for the XML properties of that schema;
3. Use XLINQ to easily load and query some XML
Ready? Here we go!
Setup
First, make sure you have downloaded and installed Visual Studio 2008 Beta 2.
Then, in order to make inferring the XML Schema easier, download and install this XML to Schema Inference Wizard, which is an add-on to Visual Studio and makes the process of inferring a schema from XML very easy.
We will be querying a XML file that contains data from a plant catalog. To obtain this file, go to http://www.w3schools.com/xml/xml_examples.asp and download the file plant_catalog.xml to your desktop.
Next, launch Visual Studio 2008 Beta 2 and create a Console Application using the Visual Basic language:
Add a XML namespace to the XML file
In the same way that the .NET framework organizes the massive API it encompasses into namespaces to establish an organization for the framework and minimize name collisions, when we use XML using these tools, it helps to add a XML namespace if the file does not already have one. This XML namespace is used by visual studio to map the schema to the actual file you are working with. The namespace must be in the form of a URI, which means it can be either a URL or a URN. In a production environment, you would agree with your development team or department what the namespace would be, but for our example, it could be anything. To add the namespace to the file you downloaded, open it in notepad and extend line 3 of the line to read as follows:
Think of this as analogous to the code you would write when importing a .NET namespace into your procedural code. Placing this namespace in the file provides a unique reference point for the schema we will infer.
Infer the Schema
This part is fun. Right-click on the project in the solution explorer and choose “Add new item”. Then from the dialog box (if you have installed the XML to Schema Inference Wizard), you will see at the bottom an item called “Xml to Schema”. Select that, and for the purposes of our demo, name it “PlantCatalog.xsd”.

The wizard will run and the following dialog box is displayed:
Note that you can infer the schema with this wizard from:
- A file on your disk
- A location on the web
- The contents of your clipboard (Add as XML)
In our case, we will “Add from File”. When you click on the “Add from File” button, a dialog will appear, where you should navigate to the plant_catalog.xml file referred to earlier in this post:
The dialog will then list the file as an input to be processed by the wizard, as follows:
Click OK and you will see that a file, “PlantCatalog.xsd” is created in the solution explorer:
Open this file by double-clicking to review the contents:
The actual work of inferring and building the schema is done by a tool that has shipped with Visual Studio since VS.NET 2003, named xsd.exe, however, this is a command-line tool while the XML to Schema Inference Wizard we are using puts a nice UI over this step and also takes care of adding the output to the current project.
Note also that the “targetnamespace” property has been inferred from the xmlns property we added earlier.
Importing the Schema into Intellisense and writing code
Now that we have a schema ready to use, all we need to do is tell the VB9 compiler that we want to use it in our code. For this we use the standard “Imports” directive that you are familiar with when you want to import .NET namespaces.
Choose “” from the intellisense choices and then type a colon. This colon defines a separator where you define an alias you will use in your code to refer to as an alias for this namespace. We use “plants” as the alias. Finally, if you type “=”” you will see the intellisense suggest the namespace for the XML schema you have brought into your project:
I should note here that the steps we took in this example of adding the namespace and importing it are not required if you are working only with one XML document in your application that does not specify a namespace. However, many real-world cases will have more than one schema, so this is a good thing to know.
Accept this option and the close the statement with a closing quotation and greater-than sign. You are now ready to begin writing code. Let’s use the following example that shows XLINQ in action:
Note that in addition to the normal intellisense you have come to expect as part of your programming experience with Visual Studio that you also get intellisense in places where you are using XML literals in the code, for example:
And also here:
The “…” operator deserves a bit of explanation here, as it is new for VB9. Referring again to Meijer and Beckman’s paper, (in section 5.2, actually) this syntax is defined as representing the child axis of the current element. This is a form of syntactic sugar that returns all PLANT nodes in the plantsXML document, regardless of how deeply in the hierarchy they occur. It is another aspect unique to VB9 at this time that will save a lot of time for those of us reading XML in code.
Finally, what you will see, if you run your lovely application is the following output:

(
read more)