Visão geral de LINQ to XML em Visual Basic

Visual BasicFornece suporte para LINQ to XML por meio de literais XML e propriedades de eixo XML . Isso permite que você use uma sintaxe conveniente e familiar para trabalhar com XML no seu Visual Basic código. LiteraisXML permitem que você incluir o XML diretamente em seu código. XML axis properties enable you to access child nodes, descendant nodes, and attributes of an XML literal. For more information, see Visão geral dos literais XML (Visual Basic) and Acessando XML no Visual Basic.

LINQ to XMLé no-API de programação deXML dememóriaprojetada especificamente para tirar proveito dos LINQ (consulta integrada à linguagem). Embora você pode chamar o LINQ APIs diretamente, apenas Visual Basic permite que você declare literais XML e acessar diretamente o propriedades de eixo XML .

ObservaçãoObservação

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 para vídeo Para demonstrações de vídeo relacionadas, consulte como faço para começar com o LINQ XML? e como faço para criar planilhas do Excel usando o LINQ XML?.

Creating XML

There are two ways to create XML trees in Visual Basic. Você pode declarar um XML literal diretamente no código, ou você pode usar o LINQ APIs para criar a árvore. 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 Criando XML em Visual Basic.

Accessing and Navigating XML

Visual Basicfornece propriedades de eixo XML para acessar e navegar em estruturas XML . These properties enable you to access XML elements and attributes by specifying the XML child element names. Como alternativa, você pode chamar explicitamente o LINQ métodos para navegar e localizar elementos e atributos. 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 Acessando XML no Visual Basic.

XML Namespaces

Visual Basicpermite que você especifique um alias para o XML namespace global usando o Imports demonstrativo. 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 Operador GetXmlNamespace (Visual Basic).

For more information, see Instrução Imports (Namespace XML).

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)

Consulte também

Outros recursos

XML no Visual Basic

Criando XML em Visual Basic

Acessando XML no Visual Basic

Manipulação de XML no Visual Basic