Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleBuild a formatted XML document in memory by using an XmlTextWriter in VB .NET
DescriptionThis example shows how to build a formatted XML document in memory by using an XmlTextWriter in VB .NET.
KeywordsXML, VB.NET, format XML, formatted XML
CategoriesVB.NET, Internet
 
When the user clicks the Go button, the program makes a MemoryStream and an XmlTextWriter attached to it. It sets the XmlTextWriter so it indents its output and then calls its WriteStartDocument method to start the XML document.

It calls the WriteStartElement method to create the <Employees> start tag. It then calls MakeEmployee several times to make Employee elements. It closes the Employees element by calling WriteEndElement to create the </Employees> end tag. It then closes the document by calling WriteEndDocument.

To display the result, the program makes a StreamReader attached to the MemoryStream. It moves to the beginning of the stream and uses the StreamReader's ReadToEnd method to read the stream's contents.

 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Dim memory_stream As New MemoryStream
    Dim xml_text_writer As New XmlTextWriter(memory_stream, _
        System.Text.Encoding.UTF8)

    ' Use indentation to make the result look nice.
    xml_text_writer.Formatting = Formatting.Indented
    xml_text_writer.Indentation = 4

    ' Write the XML declaration.
    xml_text_writer.WriteStartDocument(True)

    ' Start the Employees node.
    xml_text_writer.WriteStartElement("Employees")

    ' Write some Employee elements.
    MakeEmployee(xml_text_writer, "Albert", "Anders", 11111)
    MakeEmployee(xml_text_writer, "Betty", "Beach", 22222)
    MakeEmployee(xml_text_writer, "Chuck", "Cinder", 33333)

    ' End the Employees node.
    xml_text_writer.WriteEndElement()

    ' End the document.
    xml_text_writer.WriteEndDocument()
    xml_text_writer.Flush()

    ' Use a StreamReader to display the result.
    Dim stream_reader As New StreamReader(memory_stream)

    memory_stream.Seek(0, SeekOrigin.Begin)
    txtResult.Text = stream_reader.ReadToEnd()
    txtResult.Select(0, 0)

    ' Close the XmlTextWriter.
    xml_text_writer.Close()
End Sub
 
Subroutine MakeEmployee uses the XmlTextWriter's WriteStartElement, WriteString, and WriteEndElement methods to build an Employee element.
 
' Add an Employee node to the document.
Private Sub MakeEmployee(ByVal xml_text_writer As _
    XmlTextWriter, ByVal first_name As String, ByVal _
    last_name As String, ByVal emp_id As Integer)
    ' Start the Employee element.
    xml_text_writer.WriteStartElement("Employee")

    ' Write the FirstName.
    xml_text_writer.WriteStartElement("FirstName")
    xml_text_writer.WriteString(first_name)
    xml_text_writer.WriteEndElement()

    ' Write the LastName.
    xml_text_writer.WriteStartElement("LastName")
    xml_text_writer.WriteString(last_name)
    xml_text_writer.WriteEndElement()

    ' Write the EmployeeId.
    xml_text_writer.WriteStartElement("EmployeeId")
    xml_text_writer.WriteString(emp_id.ToString)
    xml_text_writer.WriteEndElement()

    ' Close the Employee element.
    xml_text_writer.WriteEndElement()
End Sub
 
For more information on using XML in VB .NET, see my book Visual Basic .NET and XML.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated