24.4. Schema Support

Although VB.NET enables you to query XML using elements and attributes, it doesn't actually provide any validation that you have entered the correct element and attribute names. To reduce the chance of entering the wrong names, you can import an XML schema, which will extend the default IntelliSense support to include the element and attribute names. You import an XML schema as you would any other .NET namespace. First you need to add a reference to the XML schema to your project, and then you need to add an Imports statement to the top of your code file.

Unlike other import statements, an XML schema import can't be added in the Project Properties Designer, which means you need to add it to the top of any code file in which you want IntelliSense support.

If you are working with an existing XML file but don't have a schema handy, manually creating an XML schema just so you can have better IntelliSense support seems like overkill. Luckily, the VB.NET team has made available the XML to Schema Inference Wizard for Visual Studio 2008, which you can download free from www.microsoft.com. Once installed, this wizard gives you the ability to create a new XML schema based on an XML snippet or XML source file, or from a URL that contains the XML source. In our example, we're going to start with an XML snippet that looks like the following:

<c:Customers xmlns:c="http://www.professionalvisualstudio.com/chapter24/customers"> <c:Customer Name="Bob Jones"> <c:Order Product="Milk" ...

Get Professional Visual Studio® 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.