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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » XML » XML File Creation 




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.WriteLinestream_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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.