Bagikan melalui


XmlReader.GetAttribute Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

'Declaration
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, nulla null reference (Nothing in Visual Basic) is returned.

Exceptions

Exception Condition
ArgumentNullException

name is nulla 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

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.