Build an XML Document : XML File Creation « XML « VB.Net






Build an XML Document

Build an XML Document
 
Imports System
Imports System.Xml
Imports System.IO

Public Class MainClass

   Shared Sub Main()
        Dim memory_stream As New MemoryStream()
        Dim xml_text_writer As New XmlTextWriter(memory_stream, System.Text.Encoding.UTF8)

        xml_text_writer.Formatting = Formatting.Indented
        xml_text_writer.Indentation = 4

        xml_text_writer.WriteStartDocument(True)

        xml_text_writer.WriteStartElement("Employees")

        MakeEmployee(xml_text_writer, "A", "A", 1)
        MakeEmployee(xml_text_writer, "B", "B", 2)
        MakeEmployee(xml_text_writer, "C", "C", 3)

        xml_text_writer.WriteEndElement()

        xml_text_writer.WriteEndDocument()
        xml_text_writer.Flush()

        Dim stream_reader As New StreamReader(memory_stream)

        memory_stream.Seek(0, SeekOrigin.Begin)
        Console.WriteLine( stream_reader.ReadToEnd())

        xml_text_writer.Close()
   End Sub 

    ' Add a node to the document.
   Shared 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

End Class

           
         
  








Related examples in the same category

1.Use XmlWriter to generate XML documentUse XmlWriter to generate XML document
2.Create XML document: XmlDocument, XmlAttribute, XmlElement
3.Read and write XML with XmlReader and XmlWriter
4.Output with XmlWriter
5.Creating Xml with XmlTextWriter