Mono Class Library: System.Xml NamespaceSystem.Xml.XmlTextWriter Class |
See Also: XmlTextWriter Members
System.Object
System.Xml.XmlWriter
System.Xml.XmlTextWriter
|
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
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, where n is the literal character 'n' and i 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 string.Empty are equivalent. string.Empty follows the W3C rules.
This class implements the System.Xml.XmlWriter class.
[Edit]
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.
C# Example 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" />
Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0