Propriedade de eixo de atributo XML (Visual Basic)

Fornece acesso ao valor de um atributo para um XElement objeto ou para o primeiro elemento em uma coleção de XElement objetos.

object.@attribute
-or-
object.@<attribute>

Parts

  • object
    Required. An XElement object or a collection of XElement objects.

  • .@
    Required. Denotes the start of an attribute axis property.

  • <
    Optional. Denotes the beginning of the name of the attribute when attribute is not a valid identifier in Visual Basic.

  • attribute
    Required. Name of the attribute to access, of the form [prefix:]name.

    Part

    Description

    prefix

    Optional. XML namespace prefix for the attribute. Must be a global XML namespace defined with an Imports statement.

    name

    Required. Local attribute name. See Nomes de elementos e atributos XML declarados (Visual Basic).

  • >
    Optional. Denotes the end of the name of the attribute when attribute is not a valid identifier in Visual Basic.

Valor de retorno

Uma seqüência de caracteres que contém o valor de attribute. Se o nome do atributo não existir, Nothing é retornado.

Comentários

Você pode usar uma propriedade do eixo deatributo XMLpara acessar o valor de um atributo por nome de um XElementobjeto ou do primeiro elemento em uma coleção de XElement objetos. You can retrieve an attribute value by name, or add a new attribute to an element by specifying a new name preceded by the @ identifier.

When you refer to an XML attribute using the @ identifier, the attribute value is returned as a string and you do not need to explicitly specify the Value property.

As regras de nomeação para atributos XML são diferentes das regras de nomenclatura para Visual Basic identificadores. Para acessar umatributo XMLque tem um nome que não é válido de Visual Basic de identificador, coloque o nome em colchetes angulares (< e >).

XML Namespaces

The name in an attribute axis property can use only XML namespace prefixes declared globally by using the Imports statement. It cannot use XML namespace prefixes declared locally within XML element literals. For more information, see Instrução Imports (Namespace XML).

Exemplo

The following example shows how to get the values of the XML attributes named type from a collection of XML elements that are named phone.

' Topic: XML Attribute Axis Property
Dim phones As XElement = 
    <phones>
        <phone type="home">206-555-0144</phone>
        <phone type="work">425-555-0145</phone>
    </phones>

Dim phoneTypes As XElement = 
  <phoneTypes>
      <%= From phone In phones.<phone> 
          Select <type><%= phone.@type %></type> 
      %>
  </phoneTypes>

Console.WriteLine(phoneTypes)

This code displays the following text:

<phoneTypes>

<type>home</type>

<type>work</type>

</phoneTypes>

The following example shows how to create attributes for an XML element both declaratively, as part of the XML, and dynamically by adding an attribute to an instance of an XElement object. The type attribute is created declaratively and the owner attribute is created dynamically.

Dim phone2 As XElement = <phone type="home">206-555-0144</phone>
phone2.@owner = "Harris, Phyllis"

Console.WriteLine(phone2)

This code displays the following text:

<phone type="home" owner="Harris, Phyllis">206-555-0144</phone>

The following example uses the angle bracket syntax to get the value of the XML attribute named number-type, which is not a valid identifier in Visual Basic. 

Dim phone As XElement = 
     <phone number-type=" work">425-555-0145</phone>

 Console.WriteLine("Phone type: " & phone.@<number-type>)

This code displays the following text:

Phone type: work

The following example declares ns as an XML namespace prefix. It then uses the prefix of the namespace to create an XML literal and access the first child node with the qualified name "ns:name".

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

Class TestClass3

    Shared Sub TestPrefix()
        Dim phone = 
            <ns:phone ns:type="home">206-555-0144</ns:phone>

        Console.WriteLine("Phone type: " & phone.@ns:type)
    End Sub

End Class

This code displays the following text:

Phone type: home

Consulte também

Referência

XElement

Conceitos

Nomes de elementos e atributos XML declarados (Visual Basic)

Outros recursos

Propriedades de eixo XML (Visual Basic)

Literais XML (Visual Basic)

Criando XML em Visual Basic