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.
Assembly: System.Xml (in System.Xml.dll)
'Declaration Public Sub WriteAttributeString ( _ prefix As String, _ localName As String, _ ns As String, _ value As String _ )
The state of writer is not WriteState.Element or writer is closed.
The xml:space or xml:lang attribute value is invalid.
The localName or ns is Nothing.
An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”
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 Nothing.
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.
For the asynchronous version of this method, see WriteAttributeStringAsync.
The following example uses the WriteAttributeString method to write a namespace declaration.
Imports System Imports System.IO Imports System.Xml Public Class Sample Public Shared Sub Main() Dim writer As XmlWriter = Nothing writer = XmlWriter.Create("sampledata.xml") ' 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 and close the writer. writer.Flush() writer.Close() End Sub End Class
.NET FrameworkSupported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.