Dela via


Overview of LINQ to XML in Visual Basic

Visual Basic provides support for LINQ to XML through XML literals and XML axis properties. This enables you to use a familiar, convenient syntax for working with XML in your Visual Basic code. XML literals enable you to include XML directly in your code. XML axis properties enable you to access child nodes, descendant nodes, and attributes of an XML literal. For more information, see XML Literals Overview (Visual Basic) and Accessing XML in Visual Basic.

LINQ to XML is an in-memory XML programming API designed specifically to take advantage of Language-Integrated Query (LINQ). Although you can call the LINQ APIs directly, only Visual Basic enables you to declare XML literals and directly access XML axis properties.

Note

XML literals and XML axis properties are not supported in declarative code in an ASP.NET page. To use Visual Basic XML features, put your code in a code-behind page in your ASP.NET application.

link to video For related video demonstrations, see How Do I Get Started with LINQ to XML? and How Do I Create Excel Spreadsheets using LINQ to XML?.

Creating XML

There are two ways to create XML trees in Visual Basic. You can declare an XML literal directly in code, or you can use the LINQ APIs to create the tree. Both processes enable the code to reflect the final structure of the XML tree. For example, the following code example creates an XML element:

Dim contact1 As XElement = 
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>

For more information, see Creating XML in Visual Basic.

Accessing and Navigating XML

Visual Basic provides XML axis properties for accessing and navigating XML structures. These properties enable you to access XML elements and attributes by specifying the XML child element names. Alternatively, you can explicitly call the LINQ methods for navigating and locating elements and attributes. For example, the following code example uses XML axis properties to refer to the attributes and child elements of an XML element. The code example uses a LINQ query to retrieve child elements and output them as XML elements, effectively performing a transform.

' Place Imports statements at the top of your program.   
Imports <xmlns:ns="http://SomeNamespace">

Module Sample1

    Sub SampleTransform()

        ' Create test by using a global XML namespace prefix.  

        Dim contact = 
            <ns:contact>
                <ns:name>Patrick Hines</ns:name>
                <ns:phone ns:type="home">206-555-0144</ns:phone>
                <ns:phone ns:type="work">425-555-0145</ns:phone>
            </ns:contact>

        Dim phoneTypes = 
          <phoneTypes>
              <%= From phone In contact.<ns:phone> 
                  Select <type><%= phone.@ns:type %></type> 
              %>
          </phoneTypes>

        Console.WriteLine(phoneTypes)
    End Sub 

End Module

For more information, see Accessing XML in Visual Basic.

XML Namespaces

Visual Basic enables you to specify an alias to a global XML namespace by using the Imports statement. The following example shows how to use the Imports statement to import an XML namespace:

Imports <xmlns:ns="http://someNamespace">

You can use an XML namespace alias when you access XML axis properties and declare XML literals for XML documents and elements.

You can retrieve an XNamespace object for a particular namespace prefix by using the GetXmlNamespace Operator (Visual Basic).

For more information, see Imports Statement (XML Namespace).

Using XML Namespaces in XML Literals

The following example shows how to create an XElement object that uses the global namespace ns:

Dim contact1 As XElement = 
    <ns:contact>
        <ns:name>Patrick Hines</ns:name>
        <ns:phone type="home">206-555-0144</ns:phone>
        <ns:phone type="work">425-555-0145</ns:phone>
    </ns:contact>

Console.WriteLine(contact1)

The Visual Basic compiler translates XML literals that contain XML namespace aliases into equivalent code that uses the XML notation for using XML namespaces, with the xmlns attribute. When compiled, the code in the previous section's example produces essentially the same executable code as the following example:

Dim contact2 As XElement = 
    <ns1:contact xmlns:ns1="http://someNamespace">
        <ns1:name>Patrick Hines</ns1:name>
        <ns1:phone type="home">206-555-0144</ns1:phone>
        <ns1:phone type="work">425-555-0145</ns1:phone>
    </ns1:contact>

Console.WriteLine(contact2)

Using XML Namespaces in XML Axis Properties

XML namespaces declared in XML literals are not available for use in XML axis properties. However, global namespaces can be used with the XML axis properties. Use a colon to separate the XML namespace prefix from the local element name. Following is an example:

Console.WriteLine("Contact name is: " & contact1.<ns:name>.Value)

See Also

Other Resources

XML in Visual Basic

Creating XML in Visual Basic

Accessing XML in Visual Basic

Manipulating XML in Visual Basic