Writing XML Attributes


In this section we examine how to write attributes on elements. We also look at declaring namespace prefixes and creating the xml:space and xml:lang attributes.

Just like elements, attributes can be constructed piece by piece or written all at once. Writing attributes piece by piece is similar to writing an element piece by piece. The WriteStartAttribute and WriteEndAttribute methods provide this functionality.

WriteStartAttribute writes the start of an attribute, "name=", or a start attribute with an optional user -defined namespace prefix, "prefix: name =". WriteStartAttribute needs to be followed by a call to some method that will write the content of the attribute. The WriteEndAttribute method should be called after writing the attribute value to signal that the attribute is completely written.

The WriteStartAttribute method will raise an InvalidOperationException if the writer's WriteState property is not equal to WriteState.Element . WriteEndAttribute will throw a System.InvalidOperationException , as well, if the writer's WriteState is not equal to WriteState.Attribute .

If a namespace URI and prefix is provided to the WriteStartAttribute method, a namespace prefix will be declared along with the attribute being written. It is important to remember that attributes do not pick up default namespaces. Therefore, attributes must be explicitly prefixed with a namespace prefix to be considered "in" a namespace. The code in Listing 10.25 demonstrates how to build an attribute piece by piece.

Listing 10.25
 C# public static void Main() {   XmlTextWriter writer =     new XmlTextWriter("startatt.xml", Encoding.UTF8);   writer.Formatting = Formatting.Indented;   writer.WriteStartElement("root");   writer.WriteStartAttribute("po", "att1", "http://bogus");   writer.WriteString("value");   writer.WriteEndAttribute();   writer.WriteEndElement();   writer.Close(); } VB sub Main()   Dim writer As     New XmlTextWriter("startatt.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.WriteStartElement("root")   writer.WriteStartAttribute("po", "att1", "http://bogus")   writer.WriteString("value")   writer.WriteEndAttribute()   writer.WriteEndElement()   writer.Close() End Sub Output <root po:att1="value" xmlns:po="http://bogus" /> 

The XmlTextWriter also supplies the WriteAttributeString method to write an attribute with one method call. This method writes out the attribute, as well as any necessary namespace declarations. This method also performs a couple of checks to ensure that the attribute content will be well- formed . If the attribute value includes double or single quotes, they will be replaced with &quot; and &apos; , respectively. If you are writing an xml:space attribute, the writer verifies that the attribute value is valid. Valid values include the string's "preserve" and "default." Listing 10.26 demonstrates how to use WriteAttributeString .

Listing 10.26
 C# public static void Main() {   XmlTextWriter writer =     new XmlTextWriter("attstring.xml", Encoding.UTF8);   writer.Formatting = Formatting.Indented;   writer.WriteStartElement("root");   writer.WriteAttributeString("att1", "http://bogus", "value1");   writer.WriteEndElement();   writer.Close(); } VB sub Main()   Dim writer As     New XmlTextWriter("attstring.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.WriteStartElement("root")   writer.WriteAttributeString("att1", "http://bogus", "value1")   writer.WriteEndElement()   writer.Close(); End Sub Output <root d1p1:att1="value1" xmlns:d1p1="http://bogus" /> 


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