By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Sharpening Your Axis with Visual Basic 9 : Page 3

Visual Basic 9 completely eliminates the barrier between the code you write and the XML you're trying to express. Creating, querying, and transforming XML is much more intuitive and productive than ever before.

WEBINAR:

On-Demand

XDocument vs. XElement: Effect on XML IntelliSense
In the LINQ to XML API, when the variable type is XDocument, the variable is positioned outside the root element of the document so the IntelliSense list will contain all the root elements in the XML schemas that are part of the project. If the variable type is XElement it means that the element can be positioned on any element within the project's XML schemas; therefore, the IntelliSense list will contain all the child elements of all the elements in these XML schemas.

Consider the example in Figure 3 where the variable doc is an XDocument created from an XML literal. The IntelliSense list contains all the root elements in the XML schemas in the project (this case displays only the <a> element because there is only one schema). If you change the literal to be of type XElement (as shown in Figure 4) the XElement can be of any XSD type, so the IntelliSense box contains all the child elements of all the elements in the project's XML schemas, in this case <b>, <c> and <d> elements.

Figure 3. XDocument Object IntelliSense: With an XDocument object, the IntelliSense list contains the root elements from the XML schemas in the project.

Figure 4. XElement Object IntelliSense: With an XElement object, the IntelliSense list contains all the child elements from the XML schemas in the project.

One optimization that the IntelliSense engine makes is to look for the Load method when an XElement variable is created. The Load method is a shared method exposed by the XElement and XDocument classes that lets you specify the XML source location, be that a file on disk or a URL. If you initialize the variable using XElement.Load, the IntelliSense list will contain the child elements of only the root elements in the project's XML schemas, and not all elements. This is because the XElement must be one of the schema root elements itself.

For example, let's assume that the myDoc.xml file contains the same XML document as the literal in Figure 4. In Figure 5 the IntelliSense list contains only the <b> element because the XElement doc is already the root element <a>.

Importing XML Namespaces
When you work with multiple XSD schemas and XML that defines namespaces you can import the XML namespaces at the top of your Visual Basic code files in the same way you import .NET framework namespaces. Microsoft expanded the Imports keyword so you can specify an XML namespace like this:

Now you can add the Imports statement above and qualify the axis properties with the XML namespace alias when you create, query or transform your XML (see Figure 6).

Importing XML namespaces has an effect on XML IntelliSense, causing it to match both the namespace prefixes and the local names are matched as you type. This offers greater productivity when coding, because instead of typing the prefix, then the colon, and then the local name, you can simply start typing the local name.

Figure 7 shows a simple example of how it works, starting with an input document and the applicable IntelliSense. If you just type the letter "n" in this example, then IntelliSense selects the customers:name entry right away (see Figure 8). If you type the letter "c" then IntelliSense selects customers:city instead (see Figure 9), and the IntelliSense list also contains the prefix customers and the category element. The next character that you type after the "c" determines which single entry IntelliSense will select.

Figure 7. IntelliSense and Namespace Prefixes: XML IntelliSense matches both the namespace prefixes and the local names as you type.