Partilhar via


Propriedade de valor XML (Visual Basic)

Provides access to the value of the first element of a collection of XElement objects.

object.Value

Parts

Term

Definition

object

Required. Collection of XElement objects.

Valor de retorno

A String that contains the value of the first element of the collection, or Nothing if the collection is empty.

Comentários

The Value property makes it easy to access the value of the first element in a collection of XElement objects. This property first checks whether the collection contains at least one object. If the collection is empty, this property returns Nothing. Otherwise, this property returns the value of the Value property of the first element in the collection.

ObservaçãoObservação

Quando você acessa o valor de umatributo XMLusando o '@' o valor do atributo doidentificador, é retornado como um String e você não precisa especificar explicitamente o Valuepropriedade.

To access other elements in a collection, you can use the XML extension indexer property. For more information, see Propriedade Extension Indexer (Visual Basic).

Inheritance

Most users will not have to implement IEnumerable<T>, and can therefore ignore this section.

The Value property is an extension property for types that implement IEnumerable(Of XElement). A ligação dessapropriedade de extensãoé como a ligação dos métodos de extensão : Se um tipo implementa uma das interfaces e define uma propriedade que tem o nome "Valor", essa propriedade tem precedência sobre apropriedadede extensão. In other words, this Value property can be overridden by defining a new property in a class that implements IEnumerable(Of XElement).

Exemplo

The following example shows how to use the Value property to access the first node in a collection of XElement objects. The example uses the child axis property to get the collection of all child nodes named phone that are in the contact object.

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

Console.WriteLine("Phone number: " & contact.<phone>.Value)

This code displays the following text:

Phone number: 206-555-0144

The following example shows how to get the value of an XML attribute from a collection of XAttribute objects. The example uses the attribute axis property to display the value of the type attribute for all of the the phone elements.

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


Dim types = contact.<phone>.Attributes("type")

For Each attr In types
  Console.WriteLine(attr.Value)
Next

This code displays the following text:

home

work

Consulte também

Referência

XElement

IEnumerable<T>

Propriedade Extension Indexer (Visual Basic)

Propriedade de eixo filho XML (Visual Basic)

Propriedade de eixo de atributo XML (Visual Basic)

Conceitos

Métodos de extensão (Visual Basic)

Outros recursos

Propriedades de eixo XML (Visual Basic)

Literais XML (Visual Basic)

Criando XML em Visual Basic