WriteAttributeString Method (String, String, String, String)

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.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public void WriteAttributeString (
	string prefix,
	string localName,
	string ns,
	string value
)
public void WriteAttributeString (
	String prefix, 
	String localName, 
	String ns, 
	String value
)
public function WriteAttributeString (
	prefix : String, 
	localName : String, 
	ns : String, 
	value : String
)
Not applicable.

Parameters

prefix

The namespace prefix of the attribute.

localName

The local name of the attribute.

ns

The namespace URI of the attribute.

value

The value of the attribute.

Exception typeCondition

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 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 a null reference (Nothing in Visual Basic).

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.

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();  

  }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2016 Microsoft