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)
public: void WriteAttributeString( String^ prefix, String^ localName, String^ ns, String^ value )
Parameters
- prefix
-
Type:
System::String^
The namespace prefix of the attribute.
- localName
-
Type:
System::String^
The local name of the attribute.
- ns
-
Type:
System::String^
The namespace URI of 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. |
| XmlException | The localName or ns is null. |
| InvalidOperationException | 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 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.
For the asynchronous version of this method, see WriteAttributeStringAsync.
The following example uses the WriteAttributeString method to write a namespace declaration.
#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(); }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1