Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlWriter.WriteAttributeString Method (String, String)

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

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public void WriteAttributeString(
	string localName,
	string value


Type: System.String
The local name of the attribute.
Type: System.String
The value of the attribute.

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


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

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.

StringBuilder output = new StringBuilder();

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, settings))
    // Write the root element.

    // Write an element with attributes.
    writer.WriteAttributeString("date", "2/19/01");
    writer.WriteAttributeString("orderID", "136A5");

    // Write a full end element. Because this element has no
    // content, calling WriteEndElement would have written a
    // short end tag '/>'.


    // Write the XML to file and close the writer

OutputTextBlock.Text = output.ToString();


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

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.