ARTICLE

Reading XML Files in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 July 01, 2003
In this article, I will show you how to use XmlTextReader class to read an XML document and write data to the console.
Download Files:
 
Reader Level:

The XmlReader and XmlTextReader classes are defined in the System.XML namespace.

The XmlTextReader class is derived from XmlReader class. The XmlTextReader class can be used to read the XML documents. The read function of this document reads the document until end of its nodes.

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

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.

Open an XML Document.

The constructor of the XmlTextReader class opens an XML file. In this sample, I used an XML file called xmltest.xml in C\temp directory. You can download the attached file.

' Open an XML file.
Dim reader As New XmlTextReader("C:\temp\xmltest.xml").

Reading  Data.

The Read method of the XmlTextReader class reads the data.

While reader.Read().
Console.WriteLine(reader.Name).
End While

Imports System.
Imports System.Xml.
Namespace ReadXML.
End Namespace 'ReadXML.
' <summary>.
' Summary description for Class1.
' </summary>public class Class1.
Class1()
Dim Main As Integer
String
()
If (True) Then
Try
' Open an XML file
Dim reader As New XmlTextReader("C:\temp\xmltest.xml")
While reader.Read()
Console.WriteLine(reader.Name)
End While
Catch
e As Exception
Console.WriteLine("Exception: {0}", e.ToString())
End Try
Return
0
End If

share this article :
post comment
 

I searched this site for "write xml" and found this article: http://www.vbdotnetheaven.com/UploadFile/mahesh/WriteXmlFileInVbDotNet04222005024530AM/WriteXmlFileInVbDotNet.aspx

Posted by Mahesh Chand Dec 24, 2010

how can i Read as well as write in Xml file through Vb.net

Posted by valar mathi Dec 24, 2010

i learnt how to use XmlTextReader class to read an XML document from ur article thanks

Posted by valar mathi Dec 22, 2010

XmlTextReader class is defined in System.Xml namespace. You must import this namespace.
Before that, you must add assembly reference to your project.

Right click on project name, select Add Reference menu item and browse for System.Xml.Dll. Once that's added, then you import namespace.

Posted by Mahesh Chand Apr 20, 2009

Imports System.Xml Dim reader As XmlTextReader = Nothing reader = New XmlTextReader("D:\Jay\XML\Sample.xml") It s throwing error : Type 'XmlTextReader' is not defined. How to slove this problem in vb.net

Posted by Jayarama J Apr 20, 2009
Team Foundation Server 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.
    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
Become a Sponsor