ARTICLE

Writing to XML Files in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 July 01, 2003
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.
Download Files:
 
Reader Level:

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

share this article :
post comment
 

Error 1 'WriteElementInt16' is not a member of 'System.Xml.XmlTextWriter'.

Posted by Mohd Shaik Nov 03, 2007

Hi,

I was wondering if you knew of anyway to write files to an XML document, I'm very new to vb.net and am trying to write/convert a .csv file to .XML, and would be very greatful if you could help in anyway.

Thanking you

Posted by Amanda Sweeney Jun 29, 2006
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor