??XmlTextWriter Class
??Creating XML documents using the methods in this section is obviously not difficult. For many years, developers have created XML documents by concatenating some strings in the cache and then outputting the cached strings to a file. But creating an XML document this way only works if you make sure there aren't any tiny errors in the string. The .NET Framework provides a better way to create XML documents through the use of XMLwriter.
??The XML Writer class outputs XML data to a stream or file in a forward-only method. More importantly, XML Writer is designed to ensure that all XML data complies with the W3C XML 1.0 recommended specifications. You don't even have to worry about forgetting to write the closing tag, because XML Writer will write it for you. XmlWriter is the abstract base class for all XML writers. .NET Framework only provides one writer class----XmlTextWriter class.
?? Let’s first take a look at the differences between XML writers and old writers. The following code retains an array of string type:
StringBuilder sb = new StringBuilder('');
sb.Append('' );
foreach(string s in theArray) {
sb.Append('
sb.Append(''/ >');
}
sb.Append('');
??The code takes out the elements in the data through a loop, writes the label text and adds them to a string. The code ensures that the output content is well-formatted and pays attention to the indentation of new lines and supports namespaces. This approach may be error-free when the document structure you create is relatively simple. However, when you have to support handling directives, namespaces, indentation, formatting, and entities, the amount of code increases exponentially, and so does the likelihood of errors.
??The XML writer writing method function corresponds to every possible XML node type, which makes the process of creating XML documents more logical and less dependent on cumbersome markup languages. Figure 6 demonstrates how to use the methods of the XmlTextWriter class to connect a string data. The code is very concise, and the code using XML writer is easier to read and has a better structure.
Figure 6 Serializing a String Array
void CreateXmlFileUsingWriters(String[] theArray, string filename)
{
// Open the XML writer (with default character set)
XmlTextWriter xmlw = new XmlTextWriter(filename, null );
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument(); xmlw.WriteStartElement ('element');
xmlw.WriteAttributeString('value', s);
xmlw.WriteEndElement();
}
xmlw.WriteEndDocument(); .Close();
}
?? However, the XML writer is not a magician - it cannot fix input errors. The XML writer does not check that element and attribute names are valid, nor does it ensure that any Unicode character set used is appropriate for the current architecture's encoding set. As mentioned above, in order to avoid output errors, non-XML characters must be eliminated. But the writer does not provide this method.
??In addition, when creating an attribute node, Writer will not check whether the name of the attribute node is the same as the name of an existing element node. Finally, the XmlWriter class is not a verified Writer class, and it does not guarantee whether the output conforms to the schema or DTD. The writer class with validation in the .NET Framework is not currently provided. But in my book "Applied XML Programming for Microsoft .NET (Microsoft Press®, 2002)", I wrote a Writer component with verification. You can download the source code from the following URL: http://www.microsoft.com/MSPress/books/6235.asp.
?? Figure 7 lists some status values (state) of the XML writer. These values are derived from the WriteState enumeration class. When you create a Writer, its initial state is Start, indicating that you are going to configure the object. In fact, the writer has not been started. The next state is Prolog, which is set when you call the WriteStartDocument method to start working. Then, the state transition depends on the document you write and the content of the document. Prolog state is retained until you add a non-element node, such as annotation elements, processing instructions, and document types. When the first node, which is the root node, is written, the status changes to Element. The state is converted to Attribute when you call the WriterStartAtribute method, not when you call the WriteAtributeString method to write an attribute. In that case, the status should be Element. When you write a closing tag (>), the state is converted to Content. When you finish writing the document, call the WriteEndDocument method, and the status will return to Start until you start writing another document or turn off the Writer.
The above is the content of easily processing XML data (4-1) in .NET Framework. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!