Writing xml:space and xml:lang Attributes


Writing xml:space and xml:lang Attributes

The WriteAttributeString method can also be used to write the special XML attributes xml:space and xml:lang . The xml:space attribute determines how white space should be handled within an element's scope. The xml:space attribute has only two possible values: preserve and default . default signals that the parse's white-space processing mode is acceptable for this element. preserve means that all the white space in the element's scope, significant or insignificant, should be preserved. When setting the value of the xml:space attribute, the XmlTextWriter will verifiy that the value is equal to one of the two strings. If it isn't, a System.InvalidOperationException is raised.

The xml:lang attribute specifies the language of the text content within the element's scope. This can be helpful when reading XML data that may be written in multiple languages. The XmlTextWriter makes no attempt to verify that the attribute value is valid. Listing 10.27 demonstrates how to set values for both special attributes.

DECLARING NAMESPACES BY USING WriteAttributeString

WriteAttributeString actually has two usages. The first is overload of the WriteAttributeString method, which allows you to generate namespace declarations and redefine the default namespace. The code that follows demonstrates this functionality.

 
 C# public static void Main() {   XmlTextWriter writer =     new XmlTextWriter("nsdecl.xml", Encoding.UTF8);   writer.Formatting = Formatting.Indented;   writer.WriteStartElement("root");   // redefine the default namespace   writer.WriteAttributeString("xmlns",                               null,                               "http://default");   // define a namespace prefix "po"   writer.WriteAttributeString("xmlns",                               "po",                               null,                               "http://post_office");   writer.WriteEndElement();   writer.Close(); } VB sub Main()   Dim writer As     New XmlTextWriter("nsdecl.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.WriteStartElement("root")  ' redefine the default namespace   writer.WriteAttributeString("xmlns", _                               nothing, _                               "http://default")   ' define a namespace prefix "po"   writer.WriteAttributeString("xmlns", _                               "po", _                               nothing, _                               "http://post_office")   writer.WriteEndElement()   writer.Close() End Sub Output <root xmlns="http://default" xmlns:po="http://post_office" /> 

This code is worth a closer look. The default namespace for "root" and all of its child elements is redefined by calling WriteAttributeString and specifying "xmlns" as the local name of the attribute, null as the attribute namespace, and the namespace URI string as the attribute value.

The namespace prefix "po" is declared and associated with the namespace URI http://post_office by calling WriteAttributeString and specifying "xmlns" as the attribute prefix, po as the name of the attribute, null as the attribute namespace, and http://post_office as the attribute value.


Listing 10.27
 C# public static void Main() {    XmlTextWriter writer =      new XmlTextWriter("nsdecl.xml", Encoding.UTF8);    writer.Formatting = Formatting.Indented;    writer.WriteStartElement("root");    // set the xml:space attribute to preserver    writer.WriteAttributeString("xml",                                "space",                                null,                                "preserve");   // set the xml:lang attribute to lang:en   writer.WriteAttributeString("xml",                               "lang",                               null,                               "en");   writer.WriteEndElement();   writer.Close(); } VB sub Main()   Dim writer As     New XmlTextWriter("nsdecl.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.WriteStartElement("root")   ' set the xml:space attribute to preserve   writer.WriteAttributeString("xml", _                               "space", _                               nothing, _                               "preserve")  ' set the xml:lang attribute to lang:en  writer.WriteAttributeString("xml", _                              "lang", _                              nothing, _                              "en")  writer.WriteEndElement()  writer.Close() End Sub Output <root xml:space="preserve" xml:lang="en" /> 

The xml:space attribute is set to preserve by calling WriteAttributeString and specifying "xml" as the prefix, "space" as the attribute's local name, null as the namespace, and "preserve" as the attribute value. The xml:lang attribute is set to en , almost exactly as we set xml:space , except that the attribute's local name was "lang" and the value was "en" .



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net