Compartir a través de


XML Attribute Axis Property (Visual Basic)

Provides access to the value of an attribute for an XElement object or to the first element in a collection of XElement objects.

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 Names of Declared XML Elements and Attributes (Visual Basic).

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

Return Value

A string that contains the value of attribute. If the attribute name does not exist, Nothing is returned.

Remarks

You can use an XML attribute axis property to access the value of an attribute by name from an XElement object or from the first element in a collection of XElement objects. 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.

The naming rules for XML attributes differ from the naming rules for Visual Basic identifiers. To access an XML attribute that has a name that is not a valid Visual Basic identifier, enclose the name in angle brackets (< and >).

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 Imports Statement (XML Namespace).

Example

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

See Also

Reference

XElement

Concepts

Names of Declared XML Elements and Attributes (Visual Basic)

Other Resources

XML Axis Properties (Visual Basic)

XML Literals (Visual Basic)

Creating XML in Visual Basic