Bagikan melalui


XML IntelliSense in Visual Basic

The Visual Basic Code Editor includes IntelliSense features for XML that provide word completion for elements defined in an XML schema. If you include an XML Schema Definition (XSD) file in your project and import the target namespace of the schema by using the Imports statement, the Code Editor will include elements from the XSD schema in the IntelliSense list of valid member variables for XElement and XDocument objects. The following illustration shows the IntelliSense members list for an XElement object.

XML IntelliSense

XML IntelliSense in Visual Basic

Enabling XML IntelliSense in Visual Basic

To enable XML IntelliSense in Visual Basic, you must include an XSD schema file in your Visual Basic project. You must also import the target namespace for the XSD schema into your code file by using the Imports statement. Alternatively, you can add the target namespace to the project-level namespace list by using the References page of the Visual Basic Project Designer. For examples, see How to: Enable XML IntelliSense in Visual Basic. For more information, see Imports Statement (XML Namespace) and References Page, Project Designer (Visual Basic).

Note that by default you cannot see XSD schema files in Visual Basic projects. You may have to click the Show All Files button to select an XSD file to include in your project.

Generating a Schema File (Schema Inference)

You can create an XSD schema for an existing XML file by inferring the XSD schema by using Visual Studio XML tools.

  • Starting in SP1, you can use the XML to Schema Wizard to create an XML Schema set that is inferred from one or more XML documents and include it your project. You can use any combination of XML documents in the form of text files, XML from HTTP Internet addresses, or XML that is typed or pasted into the XML to Schema Wizard. To access the XML to Schema Wizard, click Add New Item on the Project menu and add an XML to Schema template from either the Data or Common Items template group. After you have included all the XML document sources to infer the XML Schema set from, click OK to create the inferred schema set. For more information, see XML to Schema Wizard (Visual Basic).

  • You can also use the Visual Studio XML Editor to infer an XSD schema set from an XML file. To create an XML schema set by using the XML Editor, open an XML file in the Visual Studio XML Designer and then click Create Schema on the XML menu. After you create the XSD schema set, you can save the created schema set to one or more XSD files and include them in your project. For more information, seeHow to: Enable XML IntelliSense in Visual Basic.

Note that different XSD schema sets might be inferred from multiple XML documents that are intended to have the same schema. This can occur when particular elements and attributes are found in one XML file and not in another, or when elements are included in different order, for example. You should review inferred XSD schema sets for completeness and accuracy when you use XSD schema inference.

Member List

After you type a period (.) to delimit an instance of an XElement or XDocument object (or an instance of IEnumerable(Of XElement) or IEnumerable(Of XDocument)), Visual Basic IntelliSense displays a list of possible object members. The initial list includes three options that represent XML axis properties, as described in the following list.

Option

Description

< >

Select this option to show a list of possible child elements. For more information, see XML Element Literal (Visual Basic) and the Elements method.

@

Select this option to show a list of possible attributes. For more information, see XML Axis Properties (Visual Basic).This option is available only for objects of type XElement.

…< >

Select this option to show a list of possible descendant elements. For more information, see How to: Access XML Descendant Elements (Visual Basic) and the Elements method.

Select or begin typing any of the XML options from the list. The member list will then display potential members from the XML schema that are specific to the selected option. If you have XML namespaces imported that are associated with a specific XML namespace prefix, a list of potential XML namespace prefixes is included in the member list.

For example, consider the following XSD schema.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           targetNamespace="http://SamplePurchaseOrder" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PurchaseOrders">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="PurchaseOrder">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Address" />
              <xs:element name="Items" />
              <xs:element name="Comment" />
            </xs:sequence>
            <xs:attribute name="PurchaseOrderNumber" type="xs:unsignedShort" use="required" />
            <xs:attribute name="OrderDate" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Valid XML for the XSD schema would resemble the following.

<?xml version="1.0"?>
<PurchaseOrders xmlns="http://SamplePurchaseOrder">
  <PurchaseOrder PurchaseOrderNumber="12345" OrderDate="2000-1-1">
    <Address />
    <Items />
    <Comment />
  </PurchaseOrder>
</PurchaseOrders>

If you include this XSD schema file in a project and import the target namespace from the XSD schema into your code file or project, Visual Basic IntelliSense displays members from the schema as you type your Visual Basic code. If the target namespace for the XSD schema is imported as the default namespace and you type the following, IntelliSense displays a list of possible child elements for the PurchaseOrder XML element.

Dim po = <PurchaseOrder />
po.<

The list consists of the Address, Comment, and Items elements.

Certainty Levels for IntelliSense List Items

Determining the XSD type to use for IntelliSense is not exact. As a result, XML IntelliSense will often show an expanded list of possible members. To aid you in selecting an item from the IntelliSense member list, items are displayed with an indication of the level of certainty that XML IntelliSense has for a particular member.

Sometimes XML IntelliSense can identify a specific type from the XSD schema. In these cases, it will display possible child elements, attributes, or descendant elements for that XSD type with a high degree of certainty. These items are identified with a check mark.

However, sometimes XML IntelliSense is not able to identify a specific type from the XSD schema. In these cases, it will display an expanded list of possible child elements, attributes, or descendant elements from the XSD schema for the project with a low degree of certainty. These items are identified with a question mark.

See Also

Tasks

How to: Enable XML IntelliSense in Visual Basic

Reference

XML to Schema Wizard (Visual Basic)

Imports Statement (XML Namespace)

XML Element Literal (Visual Basic)

XML Attribute Axis Property (Visual Basic)

XML Descendant Axis Property (Visual Basic)

References Page, Project Designer (Visual Basic)