XmlReader.GetAttribute Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

When overridden in a derived class, gets the value of the attribute with the specified Name.

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)

Syntax

Public MustOverride Function GetAttribute ( _
    name As String _
) As String
public abstract string GetAttribute(
    string name
)

Parameters

Return Value

Type: System..::.String
The value of the specified attribute. If the attribute is not found or the value is String.Empty, nullNothingnullptra null reference (Nothing in Visual Basic) is returned.

Exceptions

Exception Condition
ArgumentNullException

name is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

This method does not move the reader.

If the reader is positioned on a DocumentType node, this method can be used to get the PUBLIC and SYSTEM literals, for example, reader.GetAttribute("PUBLIC")

Examples

Dim output As New StringBuilder()

Dim xmlString As String = _
        "<PurchaseOrder>" & _
            "<Items>" & _
                "<Item PartNumber='872-AA'>" & _
                    "<ProductName>Lawnmower</ProductName>" & _
                    "<Quantity>1</Quantity>" & _
                    "<USPrice>148.95</USPrice>" & _
                    "<Comment>Confirm this is electric</Comment>" & _
                "</Item>" & _
            "</Items>" & _
        "</PurchaseOrder>"

' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
    reader.ReadToFollowing("Item")
    OutputTextBlock.Text = reader.GetAttribute("PartNumber")
End Using
StringBuilder output = new StringBuilder();

String xmlString =
    @"<PurchaseOrder>
     <Items>
      <Item PartNumber='872-AA'>
        <ProductName>Lawnmower</ProductName>
        <Quantity>1</Quantity>
        <USPrice>148.95</USPrice>
        <Comment>Confirm this is electric</Comment>
      </Item>
    </Items>
  </PurchaseOrder>";

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("Item");
    OutputTextBlock.Text = reader.GetAttribute("PartNumber");
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

XmlReader Class

GetAttribute Overload

System.Xml Namespace