As you saw in the "Understanding Microsoft .NET and XML" section, the XmlWriter class contains methods and properties to write to XML documents, and XmlTextWriter and XmlNodeWriter come from the XmlWriter class. Figure 6-3 shows XmlWriter and its derived classes.
Figure 6-3: The XmlWriter class and its derived classes
Besides providing a constructor and three properties (WriteState, XmlLang, and XmlSpace), the XmlWriter classes have many WriteXXX methods to write to XML documents. The following sections discuss some of these class methods and properties and use them in examples of the XmlTextWriter and XmlNodeWriter classes. XmlTextWriter creates a write object and writes to the documents. The XmlTextWriter constructor can take three types of inputs: a string, stream, or TextWriter.
The XmlWriter class contains three properties: WriteState, XmlLang, and XmlSpace. The WriteState property gets the current state of the XmlWriter class. The values could be Attribute, Start, Element, Content, Closed, or Prolog. The return value WriteState.Start means the Write method has not been called yet. In other cases, it represents what's being written. For example, the return value WriteState.Attribute means the Attribute value has been written. WriteState.Close represents that the stream has been closed by calling Close method.
As discussed earlier, an XML document can have many types of items including elements, comments, attributes, and whitespaces. Although it's not possible to describe all the WriteXXX methods here, we cover the most important ones.
The WriteStartDocument and WriteEndDocument methods open and close a document for writing, respectively. You must open a document before you start writing to it. The WriteComment method writes comments to a document. It takes only one string type: argument. The WriteString method writes a string to a document. With the help of WriteString, you can use the WriteStartElement and WriteEndElement method pair to write an element to a document. The WriteStartAttribute and WriteEndAttribute pair writes an attribute. WriteNode is another write method, which writes XmlReader to a document as a node of the document.
The following example summarizes these methods and creates a new XML document with some items in it such as elements, attributes, strings, comments, and so on. (See Listing 6-7 in the next section.)
Listing 6-7: XmlWriter Example
Imports System.Xml Module Module1 Sub Main() ' Create a new file c:\xmlWriterTest.xml Dim writer As XmlTextWriter = _ New XmlTextWriter("C:\\xmlWriterTest.xml", Nothing) ' Opens the document writer.WriteStartDocument() ' Write comments writer.WriteComment("This program uses XmlTextWriter.") writer.WriteComment("Developed By: Mahesh Chand.") writer.WriteComment("================================") ' Write first element writer.WriteStartElement("root") writer.WriteStartElement("r", "RECORD", "urn:record") ' Write next element writer.WriteStartElement("FirstName", "") writer.WriteString("Mahesh") writer.WriteEndElement() ' Write one more element writer.WriteStartElement("LastName", "") writer.WriteString("Chand") writer.WriteEndElement() ' Create an XmlTextReader to read books.xml Dim reader As XmlTextReader = New XmlTextReader("c:\\books.xml") While reader.Read() If reader.NodeType = XmlNodeType.Element Then ' Add node.xml to xmlWriterTest.xml usign WriteNode writer.WriteNode(reader, True) End If End While ' Ends the document. writer.WriteEndDocument() writer.Close() End Sub End Module
In this example, you create a new XML file, c:\xmlWriterText.xml, using XmlTextWriter:
' Create a new file c:\xmlWriterTest.xml Dim writer As XmlTextWriter = _ New XmlTextWriter("C:\\xmlWriterTest.xml", Nothing)
After that, add comments and elements to the document using WriteXXX methods. After that you can read the books.xml file using XmlTextReader and add its elements to xmlWriterTest.xml using XmlTextWriter:
' Create an XmlTextReader to read books.xml Dim reader As XmlTextReader = New XmlTextReader("c:\\books.xml") While reader.Read() If reader.NodeType = XmlNodeType.Element Then ' Add node.xml to xmlWriterTest.xml usign WriteNode writer.WriteNode(reader, True) End If End While
Listing 6-7 shows an example of using XmlWriter to create a new document and write its items. This program creates a new XML document, xmlWriterTest, in the C:\ root directory.
In Listing 6-7, you write output of the program to a file. If you want to write your output directly on the console, pass Console.Out as the filename when you create an XmlTextWriter object. For example: XmlTextWriter writer = new XmlTextWriter (Console.Out);.
When you open C: \xmlWriterTest.xml in a browser, the output of the program looks like Listing 6-8.
Listing 6-8: Output of the XmlWriterSample.vb Class
<?xml version="1.0" ?> - <!-- This program uses XmlTextWriter. --> - <!-- Developed By: Mahesh Chand. --> - <!-- ================================ --> - <root> - <r:RECORD xmlns:r="urn:record"> <FirstName>Mahesh</FirstName> <LastName>Chand</LastName> - <bookstore> - <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> - <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> - <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> - <book genre="philosophy" publicationdate="1991" ISBN="1-861001-56-6"> <title>The Gorgias</title> - <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore> </r:RECORD> </root>
You use the Close method when you're done with the XmlWriter object to close the stream.