Writing XML Files in VB.NET

The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on the W3C Extensible Markup Language (XML) 1.0 specification.In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.
  • 1790
Download Files:
 

The XmlWriter and XmlTextWriter classes are defined in the System.XML namespace.

The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on  the W3C Extensible Markup Language (XML) 1.0 specification.

In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.

Adding namspace Reference.

Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.

Imports System.Xml.

Creating an XML Document.

The constructor of the XmlTextWriter class creates an XML file if file doesn't exist. In this sample, I create a new XML file called xmltest.xml in C\temp directory.

Dim writer As New XmlTextWriter("C:\temp\xmltest.xml", Nothing).

NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.Out as a parameter of the constructor.

Dim writer As New XmlTextWriter(Console.Out).

Adding Data to the Document.

The WriteStartDocument method starts a new document. The WriteStartElement and the WriteEndElement pair is used to add a new element to the document. The WriteString writes a string to the document.

writer.WriteStartDocument().
writer.WriteComment("Commentss: XmlWriter Test Program").
writer.WriteProcessingInstruction("Instruction", "Person Record").
writer.WriteStartElement("p", "person", "urn:person").
writer.WriteStartElement("LastName", "").
writer.WriteString("Chand").
writer.WriteEndElement().
writer.WriteElementInt16("age", "", 25).
writer.WriteEndDocument().

Imports System
Imports System.Xml
Namespace WriteToXML
' <summary>.
' Summary description for Class1.
' </summary>.
Public Class Class1
Public Sub New()
End Sub 'New
'Entry point which delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
End
Sub
Public
Overloads Shared Function Main(ByVal args() As String) As Integer
Try
' Creates an XML file is not exist.
Dim writer As New XmlTextWriter("C:\temp\xmltest.xml", Nothing)
' Starts a new document.
writer.WriteStartDocument()
'Write comments.
writer.WriteComment("Commentss: XmlWriter Test Program").
writer.WriteProcessingInstruction("Instruction", "Person Record").
' Add elements to the file.
writer.WriteStartElement("p", "person", "urn:person").
writer.WriteStartElement("LastName", "").
writer.WriteString("Chand").
writer.WriteEndElement()
writer.WriteStartElement("FirstName", "")
writer.WriteString("Mahesh")
writer.WriteEndElement()
writer.WriteElementInt16("age", "", 25)
' Ends the document
writer.WriteEndDocument()
Catch e As Exception
Console.WriteLine("Exception: {0}", e.ToString())
End Try
Return
0
End Function 'Main
End Class 'Class1
End Namespace 'WriteToXML

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.