public class XmlTextWriter : XmlWriter
Object
XmlWriter
XmlTextWriter
System.Xml
XML
Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.
This class maintains a namespace stack corresponding to all the namespaces defined in the current element stack. Namespaces can be declared manually to override the current namespace declaration. Prefixes can be specified to associate with a namespace. If there are multiple namespace declarations mapping different prefixes to the same namespace URI, this class walks the stack of namespace declarations backwards and picks the closest one.If namespace conflicts occur inside an element, this class resolves the conflict by generating alternate prefixes. The generated prefixes are named
ni
, wheren
is the literal character 'n' andi
is a number beginning at one. The number is reset to one for each element. See the example section for a demonstration of this behavior.Attributes which are associated with a namespace URI must have a prefix (default namespaces do not apply to attributes). This conforms to section 5.2 of the W3C Namespaces in XML recommendation. If an attribute references a namespace URI, but does not specify a prefix, the writer generates a prefix for the attribute.
When writing an empty element, an additional space is added between tag name and the closing tag, for example
<item />
. This provides compatibility with older browsers.When a String is used as method parameter,
null
and System.String.Empty are equivalent. System.String.Empty follows the W3C rules.This class implements the XmlWriter class.
The following example demonstrates how this class resolves namespace conflicts inside an element. In the example, the writer writes an element that contains two attributes. The element and both attributes have the same prefix but different namespaces. The resulting XML fragment is written to the console.
using System; using System.Xml; public class WriteFragment { public static void Main() { XmlTextWriter xWriter = new XmlTextWriter(Console.Out); xWriter.WriteStartElement("prefix", "Element1", "namespace"); xWriter.WriteStartAttribute("prefix", "Attr1", "namespace1"); xWriter.WriteString("value1"); xWriter.WriteStartAttribute("prefix", "Attr2", "namespace2"); xWriter.WriteString("value2"); xWriter.Close(); } }The output is
<prefix:Element1 n1:Attr1="value1" n2:Attr2="value2" xmlns:n2="namespace2" xmlns:n1="namespace1" xnlns:prefix="namespace" />
System.Xml Namespace
XmlTextWriter Constructors
XmlTextWriter(System.String, System.Text.Encoding) Constructor
XmlTextWriter(System.IO.Stream, System.Text.Encoding) Constructor
XmlTextWriter(System.IO.TextWriter) Constructor
XmlTextWriter Methods
XmlTextWriter.Close Method
XmlTextWriter.Flush Method
XmlTextWriter.LookupPrefix Method
XmlTextWriter.WriteBase64 Method
XmlTextWriter.WriteBinHex Method
XmlTextWriter.WriteCData Method
XmlTextWriter.WriteCharEntity Method
XmlTextWriter.WriteChars Method
XmlTextWriter.WriteComment Method
XmlTextWriter.WriteDocType Method
XmlTextWriter.WriteEndAttribute Method
XmlTextWriter.WriteEndDocument Method
XmlTextWriter.WriteEndElement Method
XmlTextWriter.WriteEntityRef Method
XmlTextWriter.WriteFullEndElement Method
XmlTextWriter.WriteName Method
XmlTextWriter.WriteNmToken Method
XmlTextWriter.WriteProcessingInstruction Method
XmlTextWriter.WriteQualifiedName Method
XmlTextWriter.WriteRaw(char[], int, int) Method
XmlTextWriter.WriteRaw(System.String) Method
XmlTextWriter.WriteStartAttribute Method
XmlTextWriter.WriteStartDocument() Method
XmlTextWriter.WriteStartDocument(bool) Method
XmlTextWriter.WriteStartElement Method
XmlTextWriter.WriteString Method
XmlTextWriter.WriteSurrogateCharEntity Method
XmlTextWriter.WriteWhitespace Method
XmlTextWriter Properties
XmlTextWriter.BaseStream Property
XmlTextWriter.Formatting Property
XmlTextWriter.IndentChar Property
XmlTextWriter.Indentation Property
XmlTextWriter.Namespaces Property
XmlTextWriter.QuoteChar Property
XmlTextWriter.WriteState Property
XmlTextWriter.XmlLang Property
XmlTextWriter.XmlSpace Property
public XmlTextWriter(string filename, Encoding encoding);
Constructs and initializes a new instance of the XmlTextWriter class using the specified file.
- filename
- A String specifying the path and name of the file to write to.
- encoding
- The Encoding to generate, or
null
.
Exception Type Condition ArgumentException filename is System.String.Empty, contains only white space, or contains one or more implementation-specific invalid characters. -or-
The encoding is not supported.
ArgumentNullException filename is null
.DirectoryNotFoundException The directory path specified in filename does not exist. IOException filename includes an invalid syntax for the path or file name. PathTooLongException The specified path, file name, or both exceeds the system-defined maximum length. SecurityException The caller does not have the required permissions. UnauthorizedAccessException Write access is not permitted by the operating system for the path specified in filename.
If filename exists, it is truncated and overwritten with the new content.If encoding is
null
, the file is written as UTF-8 and the encoding attribute is omitted from the processing instruction.The following properties are initialized to the specified values:
System.Xml.XmlTextWriter.Formatting to System.Xml.Formatting.None.
System.Xml.XmlTextWriter.Indentation to 2.
System.Xml.XmlTextWriter.IndentChar to the space character.
System.Xml.XmlTextWriter.Namespaces to
true
.System.Xml.XmlTextWriter.QuoteChar to the double quote character.
System.Xml.XmlTextWriter.WriteState to System.Xml.WriteState.Start.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public XmlTextWriter(Stream w, Encoding encoding);
Constructs and initializes a new instance of the XmlTextWriter class using the specified output stream.
- w
- The Stream to write to.
- encoding
- The Encoding to generate, or
null
.
Exception Type Condition ArgumentException w cannot be written to. -or-
The encoding is not supported.
ArgumentNullException w is null
.
If encoding isnull
, the stream is written as UTF-8 and the encoding attribute is omitted from the processing instruction.The following properties are initialized to the specified values:
System.Xml.XmlTextWriter.Formatting to System.Xml.Formatting.None.
System.Xml.XmlTextWriter.Indentation to 2.
System.Xml.XmlTextWriter.IndentChar to the space character.
System.Xml.XmlTextWriter.Namespaces to
true
.System.Xml.XmlTextWriter.QuoteChar to the double quote character.
System.Xml.XmlTextWriter.WriteState to System.Xml.WriteState.Start.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public XmlTextWriter(TextWriter w);
Constructs and initializes a new instance of the XmlTextWriter class.
- w
- The TextWriter to write to, initialized to the correct encoding.
The following properties are initialized to the specified values:System.Xml.XmlTextWriter.Formatting to System.Xml.Formatting.None.
System.Xml.XmlTextWriter.Indentation to 2.
System.Xml.XmlTextWriter.IndentChar to the space character.
System.Xml.XmlTextWriter.Namespaces to
true
.System.Xml.XmlTextWriter.QuoteChar to the double quote character.
System.Xml.XmlTextWriter.WriteState to System.Xml.WriteState.Start.
[Note: If a specific encoding is necessary, set the encoding using the constructor of w before instantiating the writer.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void Close();
Closes the writer.
This method closes all elements and attributes created by the System.Xml.XmlTextWriter.WriteStartElement(System.String,System.String,System.String) and System.Xml.XmlTextWriter.WriteStartAttribute(System.String,System.String,System.String) methods, respectively, that are open when the System.Xml.XmlTextWriter.Close method is called.This method calls the System.Xml.XmlTextWriter.Flush method to flush the underlying buffered stream and then closes the stream.
This method sets the System.Xml.XmlTextWriter.WriteState to System.Xml.WriteState.Closed.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void Flush();
Clears all buffers and causes any buffered data to be written to the underlying stream.
[Note: This method overrides System.Xml.XmlWriter.Flush.]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override string LookupPrefix(string ns);
Returns the prefix defined in the current namespace scope for the specified namespace URI.
- ns
- A String specifying the namespace URI.
A String containing the corresponding prefix, or System.String.Empty if the prefix is not found and ns is the default namespace, ornull
if no matching namespace URI is found in the current scope.
Exception Type Condition ArgumentException ns is null
or System.String.Empty.
[Note: This method overrides System.Xml.XmlWriter.LookupPrefix(System.String).]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteBase64(byte[] buffer, int index, int count);
Encodes the specified binary bytes as Base64 and writes the resulting text.
- buffer
- A Byte array containing the bytes to encode.
- index
- A Int32 specifying the position within the array of the first byte to encode.
- count
- A Int32 specifying the number of bytes to encode.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentException The buffer length minus index is less than count. ArgumentOutOfRangeException index or count is less than zero. InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
[Note: Base64 encoding represents byte sequences in a text form comprised of the 65 US-ASCII characters (A-Z, a-z, 0-9, +, /, =) where each character encodes 6 bits of the binary data.For more information on Base64 encoding, see RFC 2045 (http://www.ietf.org/rfc/rfc2045).
This method overrides System.Xml.XmlWriter.WriteBase64(System.Byte[],System.Int32,System.Int32).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteBinHex(byte[] buffer, int index, int count);
Encodes the specified binary bytes as BinHex and writes the resulting text.
- buffer
- A Byte array containing the bytes to encode.
- index
- A Int32 specifying the position within the array of the first byte to encode.
- count
- A Int32 specifying the number of bytes to encode.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentException The buffer length minus index is less than count. ArgumentOutOfRangeException index or count is less than zero. InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
[Note: For information on BinHex encoding, see RFC 1741 (http://www.ietf.org/rfc/rfc1741).This method overrides System.Xml.XmlWriter.WriteBinHex(System.Byte[],System.Int32,System.Int32).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteCData(string text);
Writes out a CDATA block containing the specified text.
- text
- A String specifying the text to place inside the CDATA block.
Exception Type Condition ArgumentException The text would result in a non-well formed XML document. InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes<![CDATA[
text]]>
.If text is
null
or System.String.Empty, this method writes an empty CDATA block,<![CDATA[]]>
.[Note: This method overrides System.Xml.XmlWriter.WriteCData(System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteCharEntity(char ch);
Forces the generation of a character entity for the specified Unicode character value.
- ch
- The Char for which to generate the entity.
Exception Type Condition ArgumentException The character is in the surrogate pair character range, 0xd800 - 0xdfff, or the text would result in a non-well formed XML document. InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes the Unicode character in hexadecimal character entity reference format.[Note: This method overrides System.Xml.XmlWriter.WriteCharEntity(System.Char).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteChars(char[] buffer, int index, int count);
Writes text a buffer at a time.
- buffer
- A Char array containing the text to write.
- index
- A Int32 specifying the position within the array of the start of the text to write.
- count
- A Int32 specifying the number of characters to write.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count is less than zero. - or -
The buffer length minus index is less than count.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
[Note: This method can be used to write large amounts of text a buffer at a time.An exception is thrown if surrogate pair characters would be split across multiple buffer writes. This exception must be caught in order to continue writing the next surrogate pair characters. The XML specification defines the valid ranges for surrogate pairs.
This method overrides System.Xml.XmlWriter.WriteChars(System.Char[],System.Int32,System.Int32).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteComment(string text);
Writes out a comment containing the specified text.
- text
- A String containing the text to place inside the comment.
Exception Type Condition ArgumentException The text would result in a non-well formed XML document InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes<!--
text-->
.If text is
null
or System.String.Empty , this method writes a comment with no content,<!---->
.[Note: This method overrides System.Xml.XmlWriter.WriteComment(System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteDocType(string name, string pubid, string sysid, string subset);
Writes the document type declaration with the specified name and optional attributes.
- name
- A String specifying the name of the document type.
- pubid
- A String specifying the public identifier, which is an alternative to the system identifier.
- sysid
- A String specifying the system identifier, which is the URI of the DTD (document type definition) for the document.
- subset
- A String specifying a URI that contains markup declarations.
Exception Type Condition ArgumentException name is null
or System.String.Empty.-or-
The value for name would result in invalid XML.
InvalidOperationException This method was called outside the prolog (after the root element).
The optional attributes, pubid, sysid, and subset, are not checked for invalid characters.[Note: A document type declaration is of the following form:
<!DOCTYPE name PUBLIC "pubid" "sysid" [subset]>
This method overrides System.Xml.XmlWriter.WriteDocType(System.String,System.String,System.String,System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteEndAttribute();
Closes the attribute started with the System.Xml.XmlTextWriter.WriteStartAttribute(System.String,System.String,System.String) method.
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState is notSystem.Xml.WriteState.Attribute.
[Note: The System.Xml.XmlTextWriter.WriteStartAttribute(System.String,System.String,System.String) and System.Xml.XmlTextWriter.WriteEndElement methods also will close an open attribute if one exists when they are called.This method overrides System.Xml.XmlWriter.WriteEndAttribute.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteEndDocument();
Closes open elements and attributes and sets the System.Xml.XmlTextWriter.WriteState back to the System.Xml.WriteState.Start state.
Exception Type Condition InvalidOperationException The current instance is in the wrong WriteState, or the document does not have a root element.
This method closes all elements and attributes created by the System.Xml.XmlTextWriter.WriteStartElement(System.String,System.String,System.String) and System.Xml.XmlTextWriter.WriteStartAttribute(System.String,System.String,System.String) methods, respectively, that are open when the System.Xml.XmlTextWriter.WriteEndDocument method is called.[Note: After calling this method, the current instance can be used to write a new XML document.
This method overrides System.Xml.XmlWriter.WriteEndDocument.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteEndElement();
Closes an open element and pops the corresponding namespace scope.
Exception Type Condition InvalidOperationException No element was open, or the System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
If the open element does not contain content, it is closed as an empty element using "/>
"; otherwise an end element is written.[Note: This method overrides System.Xml.XmlWriter.WriteEndElement.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteEntityRef(string name);
Writes an entity reference with the specified name.
- name
- A String specifying the name of the entity reference.
Exception Type Condition ArgumentException A String containing the text would result in a non-well formed XML document, or name is either null
or System.String.Empty.InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes%
name;
.[Note: This method overrides System.Xml.XmlWriter.WriteEntityRef(System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteFullEndElement();
Closes an open element and pops the corresponding namespace scope.
Exception Type Condition InvalidOperationException No start tag was open, or the System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes an end element regardless of whether there is any content in the element.[Note: This method overrides System.Xml.XmlWriter.WriteFullEndElement.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteName(string name);
Writes out the specified name, ensuring it is a valid name according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
- name
- A String specifying the name to write.
Exception Type Condition ArgumentException name is null
or System.String.Empty; or name is not a valid XML Name.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
If System.Xml.XmlTextWriter.Namespaces is set totrue
, this method checks that name is also valid according to the W3C Namespaces in XML recommendation (http://www.w3.org/TR/REC-xml-names).[Note: This method overrides System.Xml.XmlWriter.WriteName(System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteNmToken(string name);
Writes out the specified name, ensuring it is a valid name token (Nmtoken) according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
- name
- A String specifying the name to write.
Exception Type Condition ArgumentException name is null
or System.String.Empty; or name is not a valid XML Nmtoken.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
[Note: This method overrides System.Xml.XmlWriter.WriteNmToken(System.String).]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteProcessingInstruction(string name, string text);
Writes out a processing instruction with the specified name and text.
- name
- A String specifying the name of the processing instruction.
- text
- A String specifying the text to include in the processing instruction.
Exception Type Condition ArgumentException The text would result in a non-well formed XML document. - or -
name is
null
or System.String.Empty.- or -
This method is being used to create an XML declaration after System.Xml.XmlTextWriter.WriteStartDocument has already been called.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method writes<?
nametext?>
.If text is
null
or System.String.Empty, this method writes a processing instruction with no text content,<?
name?>
.[Note: This method overrides System.Xml.XmlWriter.WriteProcessingInstruction(System.String,System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteQualifiedName(string localName, string ns);
Writes out the qualified name.
- localName
- A String specifying the local name to write.
- ns
- A String specifying the namespace URI to associate with localname.
Exception Type Condition ArgumentException localName is null
or System.String.Empty.-or-
System.Xml.XmlTextWriter.Namespaces is
false
, and ns is neithernull
nor System.String.Empty .-or-
localName is not a valid XML name.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
If ns maps to the current default namespace, no prefix is generated.When writing attribute values, this method generates a prefix if ns is not found. When writing element content, this method throws an exception if ns is not found.
If the current instance supports namespaces (System.Xml.XmlTextWriter.Namespaces is set to
true
), this method looks up the prefix that is in scope for the given namespace and checks that the name is valid according to the W3C Namespaces in XML recommendation (http://www.w3.org/TR/REC-xml-names).[Note: This method overrides System.Xml.XmlWriter.WriteQualifiedName(System.String,System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteRaw(char[] buffer, int index, int count);
Writes raw text from a character array.
- buffer
- A Char array containing the text to write.
- index
- A Int32 specifying the position within the array of the start of the text to write.
- count
- A Int32 specifying the number of characters to write.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count is less than zero. - or -
The buffer length minus index is less than count.
InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method does not encode any characters.[Note: This method overrides System.Xml.XmlWriter.WriteRaw(System.Char[],System.Int32,System.Int32).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteRaw(string data);
Writes raw text from a string.
- data
- A String specifying the text to write.
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
If data isnull
, System.String.Empty is written.This method does not encode any characters.
[Note: This method overrides System.Xml.XmlWriter.WriteRaw(System.Char[],System.Int32,System.Int32).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteStartAttribute(string prefix, string localName, string ns);
Writes the start of an attribute with the specified prefix and name, and associates the prefix with the specified namespace URI.
- prefix
- A String specifying the namespace prefix of the attribute.
- localName
- A String specifying the local name of the attribute.
- ns
- A String specifying the namespace URI associated with the attribute.
Exception Type Condition ArgumentException System.Xml.XmlTextWriter.Namespaces is false
for the writer, and prefix and ns are not bothnull
or System.String.Empty.InvalidOperationException The System.Xml.XmlTextWriter.WriteState is not one of the following: System.Xml.WriteState.Attribute or System.Xml.WriteState.Element.
If any of the input parameters arenull
or System.String.Empty, the start attribute is written with that parameter missing.[Note: This method overrides System.Xml.XmlWriter.WriteStartAttribute(System.String,System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteStartDocument();
Writes the XML declaration with the version "1.0" and no standalone attribute.
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState is not System.Xml.WriteState.Start.
[Note: When XmlTextWriter is instantiated, the System.Xml.XmlTextWriter.WriteState is set to System.Xml.WriteState.Start. All the "write" methods change the System.Xml.XmlTextWriter.WriteState to a value other thanStart
. Thus, if this method is not the first "write" method called, a InvalidOperationException is thrown.If System.Xml.XmlTextWriter.WriteStartDocument has been called and then the System.Xml.XmlTextWriter.WriteProcessingInstruction(System.String,System.String) method is used to create another XML declaration, a ArgumentException will be thrown.
The output of this method using an encoding equal to System.Text.Encoding.Unicode and the default System.Xml.XmlTextWriter.QuoteChar is
<?xml version="1.0" encoding="utf-16"?>
Character encoding is set when the writer is instantiated.
This method overrides System.Xml.XmlWriter.WriteStartDocument.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteStartDocument(bool standalone);
Writes the XML declaration with the version "1.0" and the standalone attribute.
- standalone
- A Boolean where
true
indicates to write "yes" as the value for the standalone attribute, andfalse
indicates to write "no".
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState is not System.Xml.WriteState.Start.
[Note: When XmlTextWriter is instantiated, the System.Xml.XmlTextWriter.WriteState is set to System.Xml.WriteState.Start. All the "write" methods change the System.Xml.XmlTextWriter.WriteState to a value other thanStart
. Thus, if this method is not the first "write" method called, a InvalidOperationException is thrown.If System.Xml.XmlTextWriter.WriteStartDocument has been called and then the System.Xml.XmlTextWriter.WriteProcessingInstruction(System.String,System.String) method is used to create another XML declaration, a ArgumentException will be thrown.
The output of this method with standalone equal to
true
, an encoding equal to System.Text.Encoding.Unicode, and using the default System.Xml.XmlTextWriter.QuoteChar is:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
Character encoding is set when the writer is instantiated.
This method overrides System.Xml.XmlWriter.WriteStartDocument.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteStartElement(string prefix, string localName, string ns);
Writes a start element with the specified name, and associates it with the given namespace and prefix.
- prefix
- A String specifying the namespace prefix of the element.
- localName
- A String specifying the local name of the element.
- ns
- A String specifying the namespace URI to associate with the element.
Exception Type Condition ArgumentException System.Xml.XmlTextWriter.Namespaces is false
for the writer, and prefix and ns are not bothnull
.InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
If ns is already in scope and has an associated prefix, the current instance will automatically write that prefix also.If any of the input parameters are
null
or System.String.Empty, the start element is written with that parameter missing.[Note: Write any attributes using the System.Xml.XmlTextWriter.WriteStartAttribute(System.String,System.String,System.String), System.Xml.XmlTextWriter.WriteString(System.String), and System.Xml.XmlTextWriter.WriteEndAttribute methods, then close the element using the System.Xml.XmlTextWriter.WriteEndElement method.
This method overrides System.Xml.XmlWriter.WriteStartElement(System.String,System.String).
]
This example demonstrates the System.Xml.XmlTextWriter.WriteStartElement(System.String,System.String,System.String) method, writing the XML to the console.
using System; using System.Xml; public class WriteXml { public static void Main() { XmlTextWriter xWriter = new XmlTextWriter(Console.Out); xWriter.WriteStartDocument(); xWriter.WriteStartElement("prefix","element", "namespace"); xWriter.WriteEndDocument(); } }The output is
<?xml version="1.0" encoding= "someencoding"?>
<prefix:element xmlns:prefix="namespace" />
The value of the encoding attribute is the encoding of the output stream of the console.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteString(string text);
Writes the specified text.
- text
- A String specifying the text to write.
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed and text is neither null
nor System.String.Empty.
This method performs the following conversions before writing the text:
If text is
- The characters '&', '<', and '>' are replaced with "&", "<", and ">", respectively.
- Character values in the range 0x-0x1F (excluding the white space characters 0x9, 0x10, and 0x13) are replaced with numeric character entities ("�" through "�x1F").
- If called in the context of an attribute value, double and single quotes are replaced with """ and "'" respectively.
null
or System.String.Empty, this method writes a text node with no data content.[Note: This method overrides System.Xml.XmlWriter.WriteString(System.String).
]
The following example demonstrates the conversions performed by this method.
using System; using System.Xml; public class WriteFrag { public static void Main() { XmlTextWriter xtWriter = new XmlTextWriter(Console.Out); xtWriter.WriteString("<1 & 2 = 3>"); } }The output is&lt;1 & 2 = 3>
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteSurrogateCharEntity(char lowChar, char highChar);
Generates and writes the surrogate character entity for the surrogate character pair.
- lowChar
- A Char containing the low surrogate. This must be a value between 0xDC00 and 0xDFFF.
- highChar
- A Char containing the high surrogate. This must be a value between 0xD800 and 0xDBFF.
Exception Type Condition ArgumentException An invalid surrogate character pair was passed. InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
This method only applies to a writer that uses the UTF-16 encoding type.The surrogate character entity is written in hexadecimal format. The range for surrogate characters is #x10000 to #x10FFFF. The following formula is used to generate the surrogate character entity:
(
highChar- 0xD800) * 0x400 + (
lowChar- 0xDC00) + 0x10000
.[Note: For both HTML and XML, the document character set (and therefore the notation of numeric character references) is based on UCS [ISO-10646]. A single numeric character reference in a source document might therefore in some cases correspond to two 16-bit units in a string (a high surrogate and a low surrogate). These 16-bit units are referred to as a surrogate pair.
For more information regarding surrogates or characters, refer to section 3.7 of the Unicode 3.0/Unicode 2.0 standard located at http://www.unicode.org, or section 2.2 of the W3C XML 1.0 Recommendation located at http://www.w3.org/TR/REC-xml#charsets .
This method overrides System.Xml.XmlWriter.WriteSurrogateCharEntity(System.Char,System.Char).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override void WriteWhitespace(string ws);
Writes the given white space.
- ws
- A String containing the white space characters.
Exception Type Condition ArgumentException ws is null
or System.String.Empty or contains non-white space characters.InvalidOperationException The System.Xml.XmlTextWriter.WriteState is System.Xml.WriteState.Closed.
[Note: This method is used to manually format a document. Use the System.Xml.XmlTextWriter.Formatting property to have the current instance format the output automatically.This method overrides System.Xml.XmlWriter.WriteWhitespace(System.String).
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public Stream BaseStream { get; }
Gets the underlying stream used by the writer.
A Stream, ornull
if the current instance does not use an underlying stream.
This property is read-only.If the current instance was constructed using a TextWriter that is a subclass of the StreamWriter class, this property is equivalent to the System.IO.StreamWriter.BaseStream property.
If the writer was constructed using a Stream, this property returns the
Stream
passed to the constructor.If the writer was constructed using a file name, this property returns the
Stream
representing the file.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public Formatting Formatting { get; set; }
Indicates how the output is formatted.
One of the members of the Formatting enumeration. The default is System.Xml.Formatting.None (no special formatting).
If this property is set to System.Xml.Formatting.Indented, child elements are indented using the System.Xml.XmlTextWriter.Indentation and System.Xml.XmlTextWriter.IndentChar properties. Only element content will be indented.[Note: Writing any text content, including System.String.Empty, puts that element into mixed content mode. Child elements do not inherit this "mixed" mode status. A child element of a "mixed" element will do indenting, unless it is also contains "mixed" content. Element content (http://www.w3.org/TR/1998/REC-xml-19980210#sec-element-content) and mixed content (http://www.w3.org/TR/1998/REC-xml-19980210#sec-mixed-content) are defined according to the XML 1.0 definitions of these terms.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public char IndentChar { get; set; }
Gets or sets the character to use for indenting when System.Xml.XmlTextWriter.Formatting is set to System.Xml.Formatting.Indented.
A Char specifying the character to use for indenting. The default is space (character code 0x20).
[Note: This property can be set to any character. To ensure valid XML, set this property to a valid white space character: 0x9, 0x10, 0x13, or 0x20.]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public int Indentation { get; set; }
Gets or sets how many indentation characters to write for each level in the hierarchy when System.Xml.XmlTextWriter.Formatting is set to System.Xml.Formatting.Indented.
A Int32 specifying the number of System.Xml.XmlTextWriter.IndentChar characters to use for each level. The default is 2.
Exception Type Condition ArgumentException The value to be set is less than zero.
Indentation is performed on the following members of XmlNodeType:DocumentType
,Element
,Comment
,ProcessingInstruction
, andCDATA
. All other node types are not affected. The XmlTextWriter class does not indent the internal DTD subset.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public bool Namespaces { get; set; }
Gets or sets a value indicating whether the writer supports namespaces.
A Boolean wheretrue
indicates the writer supports namespaces; otherwise,false
. The default istrue
.
Exception Type Condition InvalidOperationException The System.Xml.XmlTextWriter.WriteState of the current instance is not System.Xml.WriteState.Start.
This property determines whether the writer supports the XML Namespaces specification (http://www.w3.org/TR/REC-xml-names).If an attempt is made to set this property after a write operation has occurred, a InvalidOperationException is thrown.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public char QuoteChar { get; set; }
Gets or sets the character used to quote the value of an attribute.
A Char specifying the quotation mark character (" or ') used to enclose the value of an attribute. The default is the double quote.
Exception Type Condition ArgumentException The value to be set is not the single quote or double quote character.
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override WriteState WriteState { get; }
Gets the write state of the writer.
One of the members of the WriteState enumeration.
This property is read-only.[Note: This property overrides System.Xml.XmlWriter.WriteState.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override string XmlLang { get; }
Gets the language attribute,xml:lang
, specifying the language in which the content and attribute values of the current element are written.
A String containing the language attribute, ornull
if the language attribute is not specified for the element.
This property is read-only.[Note: This property overrides System.Xml.XmlWriter.XmlLang.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace
public override XmlSpace XmlSpace { get; }
Gets the white space attribute,xml:space
, specifying how white space is handled in the current element.
One of the members of the XmlSpace enumeration, or System.Xml.XmlSpace.None if the white space attribute is not specified for the element.
This property is read-only.[Note: This property overrides System.Xml.XmlWriter.XmlSpace.
]
System.Xml.XmlTextWriter Class, System.Xml Namespace