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.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » XML » XML File CreationScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.