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.
 | |
Figure 5. Optimizing Itellisense: Visual Studio optimizes the IntelliSense list when using XElement.Load(). |
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:
Imports <xmlns:customers=
"urn:microsoft:examples:customers">
For example, suppose your XML declares an XML namespace:
<?xml version="1.0"?>
<customers xmlns="urn:microsoft:examples:customers">
<customer id="GREAL">
<name>Howard Snyder</name>
...
When you infer the schema it will contain a
targetNamespace attribute:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="urn:microsoft:examples:customers"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customers">
...
 | |
Figure 6. Importing XML Namespaces: Import XML namespaces and then qualify the axis properties with the alias. |
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. |
|
 | |
Figure 8. Matching a Unique Element: Typing just the letter "n" causes IntelliSense to match the customers:name entry. |
|
 | |
Figure 9. Listing Ambiguous Entries: Typing just the letter "c" causes IntelliSense to list all the possible entries that start with "c." |
|
|