WriteAttributeString Method Writes Attributes and Namespace Declarations
The WriteAttributeString method has two different tasks. One task is to write out attributes and associate them with a user defined namespace prefix. The second task is to generate namespace declarations. If writing attributes and the localname parameter is xmlns, then this method is considered to be creating a namespace declaration.
In the following code example, the WriteAttributeString method is used to write attributes inside an element.
//Write the genre attribute.
writer.WriteAttributeString("genre", "novel"); //Write the ISBN attribute.
writer.WriteAttributeString("ISBN", "1-8630-014");
The WriteAttributeString also escapes the text content of the attribute depending on what it finds. If double quotes are used, the XmlTextWriter escapes them in the text content of the attribute value with ". If single quotes are used, it escapes the text content of the attribute value with '.
To generate namespace declarations, there is an overioaded WriteAttributeString method that allows the application to define a namespace declaration. The following code example creates two default namespaces. The first declaration binds all elements with no prefix to the first namespace declaration, while any element declared with a ''po" prefix is bound to the second namespace declaration.
// Write the default namespace, identified as xmlns with no prefix writer.WriteAttributeString("xmlns", null, "http://www.w3.org/2 0 0 0/10/XMLSchema"); // Write a namespace for the purchase order with a prefix of "po" writer.WriteAttributeString("xmlns", "po", null, "http://contoso.com/po");
Post a comment