XmlWriter.WriteAttributeString Method (String, String, String, String)

When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.

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

Syntax

'Declaration
Public Sub WriteAttributeString ( _
    prefix As String, _
    localName As String, _
    ns As String, _
    value As String _
)
public void WriteAttributeString(
    string prefix,
    string localName,
    string ns,
    string value
)

Parameters

  • prefix
    Type: System.String
    The namespace prefix of the attribute.

Exceptions

Exception Condition
InvalidOperationException

The state of writer is not WriteState.Element or writer is closed.

ArgumentException

The xml:space or xml:lang attribute value is invalid.

XmlException

The localName or ns is nulla null reference (Nothing in Visual Basic).

Remarks

This method writes out the attribute with a user defined namespace prefix and associates it with the given namespace. If the prefix is "xmlns" then this method also treats this as a namespace declaration and associates the declared prefix with the namespace URI provided in the given attribute value. In this case the ns argument can be nulla null reference (Nothing in Visual Basic).

WriteAttributeString does the following:

  • If the attribute value includes double or single quotes, they are replaced with " and ' respectively.

  • If writing an xml:space attribute, the writer verifies the attribute value is valid. (Valid values are preserve or default.)

  • If writing an xml:lang attribute, the writer does not verify that the attribute value is valid according to the W3C XML 1.0 recommendation.

Examples

Dim output As New StringBuilder()

Using writer As XmlWriter = XmlWriter.Create(output)

    ' Write the root element.
    writer.WriteStartElement("book")

    ' Write the xmlns:bk="urn:book" namespace declaration.
    writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:book")

    ' Write the bk:ISBN="1-800-925" attribute.
    writer.WriteAttributeString("ISBN", "urn:book", "1-800-925")

    writer.WriteElementString("price", "19.95")

    ' Write the close tag for the root element.
    writer.WriteEndElement()

    ' Write the XML to file.
    writer.Flush()
End Using

OutputTextBlock.Text = output.ToString()

StringBuilder output = new StringBuilder();

using (XmlWriter writer = XmlWriter.Create(output))
{
    // Write the root element.
    writer.WriteStartElement("book");

    // Write the xmlns:bk="urn:book" namespace declaration.
    writer.WriteAttributeString("xmlns", "bk", null, "urn:book");

    // Write the bk:ISBN="1-800-925" attribute.
    writer.WriteAttributeString("ISBN", "urn:book", "1-800-925");

    writer.WriteElementString("price", "19.95");

    // Write the close tag for the root element.
    writer.WriteEndElement();

    // Write the XML to file.
    writer.Flush();
}

OutputTextBlock.Text = output.ToString();

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.