As you've seen in the Microsoft .NET and XML section of this article, the XmlWriter class contains methods and properties to write to XML documents, and XmlTextWriter and XmlNodeWriter come from the XmlWriter class (see figure 6-7).
Figure 6-7. XmWriter classes
Besides providing a constructor and three properties (WriteState, XmlLang, and XmlSpace), the XmlWriter classes have many writexxx methods to write to XML documents. This section discusses some of these class methods and properties and uses them in examples of the XmlTextWriter and XmlNodeWriter classes. XmlTextWriter creates a write object and writes to the document. The XmlTextWriter constructor can take three types of inputs: a string, a stream, or a TextWriter.
Xml Writer properties
The XmlWriter class contains three properties: WriterState, XmlLang, and XmlSpace. The WriteState property gets the current state of the XmlWriter class. The values could be Attributes, Start, Element, Content, closed, or Prolog. The return value WriteState.Start means the Write method is not yet called. In otnher cases, it represents what is being written. For example, the return value WriteState.Attribute means the Attribute value has written. WriteState.Close represents that the stream has closed by calling Close method.
Writing XML Items
As discussed earlier, an XML document can have any types of items including elements, Comments, attributes, and white spaces. Although it's not possible to describe all the Writexxx methods here. I'll cover some of them.
The WriteStateDocument 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 comment to a document. It takes only one string type of 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 all 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-13 in the next section.)
In this example, you create a new XML file, c:\xmlWriterText.xml, using XmlTextWriter:
' Create a new file c:\ xmlWriterTest.Xml
Dim writer As 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 xml file using Xml TextReader and add its elements to xmlWriterTest.xml using XmlTextWriter:
' Create an XmlText Reader to read books. xml
Dim reader As New XmlTextReader("@c:" & vbBack & "ooks.xml")
If reader.NodeType = XmlNodeType.Element Then
' Add node.xml to xmlWriterTest .xml using WriteNode
Listing 6-13 shows an example of using XmlWriter to create a new document and write its items. This program creates a new XML document, xml writer Test, in the C:\root directory.
Listing 6-13 XmlWriter example
Private Shared Sub Main(ByVal args As String())
' Create a new File c:\xmlWriterTest.xml
Dim writer As New XmlTextWriter("C:\ xmlWriterTest.xml", Nothing)
' opens the document
' write comments
writer.WriteComment("This Program uses XmlTextWriter.")
writer.WriteComment("Developed by :Mahesh Chand.")
writer.WriteComment("= = = = = = = = = = = = = = =")
' write first element
writer.WriteStartElement("r", "RECORD", "urn: record")
' write next element
writer.WriteStartElement("FirstName", " ")
' write one more element
writer.WriteStartElement("LastName", " ")
' Create an XmlTextReader to read books.xml
Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")
If reader.NodeType = XmlNodeType.Element Then
' Add node.xml to xmlWriterTest.xml using WriteNode
' Ends the document.
Note: In Listing 6-13 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 file name when 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-14.
Listing 6-14. Output of XmlWriterSample.cs class
<?xml version="1.0" ?>
- <!-- This program uses xmlTextWriter. -->
- <!-- Developed by: Mahesh chand. -->
- <!-- = = = = = = = = = = = = = -->
- <r:RECORD xmlns:r="urn:record">
- <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>the Autobiography of Benjamin Franklin</title>
-<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The confidence man</title>
-<book genre="Philosophy" publicationdate="1991" ISBN="1-861001-56-6">
The close method
You use the Close method when you're done with the XmlWriter object, which closes the stream.
Hope this article would have helped you in understanding Xmlwriter Classes. See other articles on the website also for further reference.