Xml Schema Inference

LINQ to Xml and Xml literals are both powerful features but they have a limitation: Within embedded expressions or when using the literals symbols in queries, IntelliSense support is not as good as usual, especially when you need to invoke Xml attributes. This means that in most situations you need to remember and manually write code that maps attributes with no IntelliSense support. This can lead to errors, because elements and attributes within Xml documents are case-sensitive, so typing age instead of Age results in a runtime error. To solve this issue, Visual Basic offers a nice feature known as the Xml Schema Inference Wizard that is a tool integrated in the IDE and that is specific for Visual Basic. The tool allows generating an Xml schema from the Xml document you query and allows enabling IntelliSense support. First, you need an Xml document. The following is a simplified list of customers that is the base document:

image

Notice how an Xml namespace declaration has been added to the Customers node. This is important and is explained later. Save the file as Customers.xml. When you get the document, right-click the project name in Solution Explorer and select Add New Item. When the Add New Item dialog box appears, select the XML to Schema template and name it CustomersSchema.xsd, as shown in Figure 28.2.

Figure 28.2 The XML to Schema item template.

image

After you click Add, a dialog named Infer Xml Schema Set from Xml Documents appears, requiring you to specify the Xml document as the base. You are not limited to specifying existing Xml documents; you can manually write the document (or paste it from the clipboard) or specify an Xml document that is available on a network. This can be useful if you want to generate schemas for RSS feeds. Click the Add from File button and select the Customers.xml document. (You can notice that Visual Studio automatically points to the project folder making the selection easier.) Figure 28.3 shows how the dialog looks.

Figure 28.3 Selecting the Xml document for generating Xml schemas from.

image

The good news is that you are not limited to just one document; you can add multiple documents to generate multiple schemas at one time. When you finish and click OK, Visual Studio generates an .Xsd document whose content is the following:

image

The schema is simple because the structure of our starting document is simple. Notice how the schema declaration contains a targetNamespace attribute that maps the Xml namespace we provided within the original document. You can also get a visual representation of the new schema via the Xml Schema Explorer tool window that is available by clicking View, Xml Schema Explorer. The tool window is shown in Figure 28.4.

Figure 28.4 The Xml Schema Explorer tool window.

image

This window is helpful to understand the hierarchical structure of an Xml schema. For the newly created schema, notice the custom namespace at the root level and then the root document node, named Customers. Within the child node, Customer, you can see nested attributes and mappings to data types. Also notice how Visual Studio tell us that Customer is part of a one-to-many relationship (1..*).

Xml Schema Designer

Visual Studio 2010 introduces a new designer for Xml schemas that is also enabled each time you work with such documents. The editor is now based on WPF and allows generating or editing schema with a powerful designer. This tool is not described here but you can easily understand how to take advantage of its instrumentation to edit schemas at the first time you see it.

At this point you are ready to write Visual Basic code. The first thing to do is to add an Imports directive pointing to the Xml namespace. Starting from Visual Basic 2008, the Imports keyword can import Xml namespaces, too, as follows:

Imports <xmlns:ds="DelSole.Customers">

Importing Xml Namespaces

Each time you need to work with Xml documents or schemas that define an Xml namespace, you need to import that namespace. This is because otherwise you should rewrite the Xml namespace declaration every time you write an Xml element within your document. By importing the namespace, you do not need to worry anymore about Xml namespaces. Microsoft Excel’s workbooks or XAML markup code are good examples of this.

This is the point where IntelliSense can provide support for your schemas. Now you can understand why the addition of an Xml namespace within the original document was necessary. At this point you could write whatever query you need. The following query is simple but its purpose is not to show how you can query data; instead its purpose is to demonstrate IntelliSense capabilities in Xml literals after the addition of an Xml schema:

image

At the point at which you type the ...< symbols, IntelliSense shows all the available options for the schema allowing the selection of available nodes. Figure 28.5 shows IntelliSense in action.

Figure 28.5 Xml Schema Inference enables IntelliSense in LINQ queries with Xml literals.

image

As you can imagine, this is helpful because now you are not required to remember every element in the Xml document and to type it manually. And of course this works not only with Xml elements but also with Xml attributes, which is what you get when beginning to write the .@ symbols. Figure 28.6 represents such a situation.

Figure 28.6 IntelliSense also allows you to select attributes.

image

If you have multiple schemas or multiple namespaces, IntelliSense can provide support for all of them making your coding experience straightforward.

Summary

The .NET Framework offers a special LINQ provider named LINQ to Xml that enables working with Xml documents. Via the System.Xml.Linq namespace, this provider allows creating and manipulating Xml documents in an efficient way. Classes such as XDocument, XElement, XAttribute, XComment, and XDeclaration are self-explanatory and allow easy generation of Xml documents. To query Xml documents you just write LINQ queries using the unified syntax that you already know, with a few additions such as the Descendants or Attribute properties. Although efficient, System.Xml.Linq can be confusing when Xml documents become larger. Luckily Visual Basic provides the Xml literals feature that allows writing Xml markup code directly into the VB code editor. To make things real, with embedded expressions you can generate documents putting local variables, expressions, and LINQ queries within Xml literals so that you can generate Xml documents dynamically. Visual Basic also takes care of your coding experience and provides the Xml Schema Inference Wizard that generates an Xml schema starting from the Xml document you want to query with LINQ to Xml and allows enabling IntelliSense for that particular document, avoiding the risk of typos.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.106