When overridden in a derived class, writes an attribute with the specified local name, namespace URI, and value.
Assembly: System.Xml (in System.Xml.dll)
Public Sub WriteAttributeString ( _ localName As String, _ ns As String, _ value As String _ )
public void WriteAttributeString( string localName, string ns, string value )
public: void WriteAttributeString( String^ localName, String^ ns, String^ value )
member WriteAttributeString : localName:string * ns:string * value:string -> unit
Parameters
- localName
- Type: System.String
The local name of the attribute.
- ns
- Type: System.String
The namespace URI to associate with the attribute.
- value
- Type: System.String
The value of the attribute.
| 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. |
This method writes out the attribute with a user defined namespace prefix and associates it with the given namespace. If localName is "xmlns" then this method also treats this as a namespace declaration. In this case, the ns argument can be null.
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.
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
using System; using System.IO; using System.Xml; public class Sample { public static void Main() { XmlWriter writer = null; writer = XmlWriter.Create("sampledata.xml"); // 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 and close the writer. writer.Flush(); writer.Close(); } }
#using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; void main() { XmlWriter^ writer = nullptr; writer = XmlWriter::Create( L"sampledata.xml" ); // Write the root element. writer->WriteStartElement( L"book" ); // Write the xmlns:bk="urn:book" namespace declaration. writer->WriteAttributeString( L"xmlns", L"bk", nullptr, L"urn:book" ); // Write the bk:ISBN="1-800-925" attribute. writer->WriteAttributeString( L"ISBN", L"urn:book", L"1-800-925" ); writer->WriteElementString( L"price", L"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(); }
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.