XmlTextWriter::WriteStartAttribute Method (String^, String^, String^)
.NET Framework (current version)
Writes the start of an attribute.
Assembly: System.Xml (in System.Xml.dll)
public: virtual void WriteStartAttribute( String^ prefix, String^ localName, String^ ns ) override
Parameters
- prefix
-
Type:
System::String^
Namespace prefix of the attribute.
- localName
-
Type:
System::String^
LocalName of the attribute.
- ns
-
Type:
System::String^
NamespaceURI of the attribute
| Exception | Condition |
|---|---|
| ArgumentException | localName is either null or String.Empty. |
Note |
|---|
Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter::Create method and the XmlWriterSettings class to take advantage of new functionality. |
This is a more advanced version of WriteAttributeString that allows you to write an attribute value using multiple write methods, such as WriteString.
The following example writes out a book.
#using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; int main() { String^ filename = "sampledata.xml"; XmlTextWriter^ writer = gcnew XmlTextWriter( filename, nullptr ); //Use indenting for readability. writer->Formatting = Formatting::Indented; writer->WriteComment( "sample XML fragment" ); //Write an element (this one is the root). writer->WriteStartElement( "bookstore" ); //Write the namespace declaration. writer->WriteAttributeString( "xmlns", "bk", nullptr, "urn:samples" ); writer->WriteStartElement( "book" ); //Lookup the prefix and then write the ISBN attribute. String^ prefix = writer->LookupPrefix( "urn:samples" ); writer->WriteStartAttribute( prefix, "ISBN", "urn:samples" ); writer->WriteString( "1-861003-78" ); writer->WriteEndAttribute(); //Write the title. writer->WriteStartElement( "title" ); writer->WriteString( "The Handmaid's Tale" ); writer->WriteEndElement(); //Write the price. writer->WriteElementString( "price", "19.95" ); //Write the style element. writer->WriteStartElement( prefix, "style", "urn:samples" ); writer->WriteString( "hardcover" ); writer->WriteEndElement(); //Write the end tag for the book element. writer->WriteEndElement(); //Write the close tag for the root element. writer->WriteEndElement(); //Write the XML to file and close the writer. writer->Flush(); writer->Close(); //Read the file back in and parse to ensure well formed XML. XmlDocument^ doc = gcnew XmlDocument; //Preserve white space for readability. doc->PreserveWhitespace = true; //Load the file doc->Load( filename ); //Write the XML content to the console. Console::Write( doc->InnerXml ); }
.NET Framework
Available since 1.1
Available since 1.1
Show:
