Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » VB.NET » XML Programming using VB.NET: Part 1

XML Programming using VB.NET: Part 1


This article explains about the XML Prograaming using VB.NET. In this article we will explore XML classes and namespaces of .NET Framework Class Library and how to use them in VB.NET to read, write and navigate XML documents.

Author Rank:
Total page views :  142409
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

XML has been an integral part of programming these days since the world of programming is moving towards the web. XML is one of the most useful easier ways to store and transfer data. Microsoft .NET framework utilizes XML to store data and transfer data between applications and remote systems including local, intranet or on the Internet.

In this article we will explore XML classes and namespaces of .NET Framework Class Library and how to use them in VB.NET to read, write and navigate XML documents.

XML Namespaces and Classes.

Microsoft .NET Runtime Library contains many classes to work with XML documents. These classes are stored in five namespaces - System.Xml, System.Xml.Schema, System.Xml.Serialization, System.Xml.XPath, and System.Xml.Xsl. The purpose of each of these namespace is different. All classes reside in one single assembly System.Xml.dll assembly.

Before you proceed to use XML classes in your programs, you need to add reference to the System.Xml.dll assembly and use namespaces in your program by using Imports keyword.

First namespace is System.Xml. This namespace contains major classes. This namespace contains many classes to read and write XML documents. In this article, we are going to concentrate on reader and write class. These reader and writer classes are used to read and write XMl documents. These classes are - XmlReader, XmlTextReader, XmlValidatingReader, XmlNodeReader, XmlWriter, and XmlTextWriter. As you can see there are four reader and two writer classes.

The XmlReader class is an abstract bases classes and contains methods and properties to read a document. The Read method reads a node in the stream. Besides reading functionality, this class also contains methods to navigate through a document nodes. Some of these methods are MoveToAttribute, MoveToFirstAttribute, MoveToContent, MoveToFirstContent, MoveToElement and MoveToNextAttribute. ReadString, ReadInnerXml, ReadOuterXml, and ReadStartElement are more read methods. This class also has a method Skip to skip current node and move to next one. We'll see these methods in our sample example.

The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. As their name explains, they are used to read text, node, and schemas.

The XmlWrite class contains functionality to write data to XML documents. This class provides many write method to write XML document items. This class is base class for XmlTextWriter class, which we'll be using in our sample example.

The XmlNode class plays an important role. Although, this class represents a single node of XML but that could be the root node of an XML document and could represent the entire file. This class is an abstract base class for many useful classes for inserting, removing, and replacing nodes, navigating through the document. It also contains properties to get a parent or child, name, last child, node type and more. Three major classes derived from XmlNode are XmlDocument, XmlDataDocument and XmlDocumentFragment. XmlDocument class represents an XML document and provides methods and properties to load and save a document. It also provides functionality to add XML items such as attributes, comments, spaces, elements, and new nodes. The Load and LoadXml methods can be used to load XML documents and Save method to save a document respectively. XmlDocumentFragment class represents a document fragment, which can be used to add to a document. The XmlDataDocument class provides methods and properties to work with ADO.NET data set objects.

In spite of above discussed classes, System.Xml namespace contains more classes. Few of them are XmlConvert, XmlLinkedNode, and XmlNodeList.

Next namespace in Xml series is System.Xml.Schema. It classes  to work with XML schemas such XmlSchema, XmlSchemaAll, XmlSchemaXPath, XmlSchemaType.

The System.Xml.Serialization namespace contains classes that are used to serialize objects into XML format documents or streams.

The System.Xml.XPath Namespce contains XPath related classes to use XPath specifications. This namespace has following classes-XPathDocument, XPathExression, XPathNavigator, and XPathNodeIterator.

With the help of XpathDocument, XpathNavigator provides a fast navigation though XML documents. This class contains many Move methods to move through a document.

The System.Xml.Xsl namespace contains classes to work with XSL/T transformations.

Reading XML Documents.

In my sample application, I'm using books.xml to read and display its data through XmlTextReader. This file comes with VS.NET samples. You can search this on your machine and change the path of the file in the following line:

Dim textReader As XmlTextReader = New XmlTextReader("books.xml")

Or you can use any XML file.

The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. Besides XmlReader methods and properties, these classes also contain members to read text, node, and schemas respectively.

I am using XmlTextReader class to read an XML file. You read a file by passing file name as a parameter in constructor.

Dim textReader As XmlTextReader = New XmlTextReader("books.xml")

After creating an instance of XmlTextReader, you call Read method to start reading the document. After read method is called, you can read all information and data stored in a document. XmlReader class has properties such as Name, BaseURI, Depth, LineNumber an so on.

List 1 reads a document and displays a node information using these properties.

List 1: Reading an XML document Properties.

' Import System.Xml namespace
Imports System.Xml
Module Module1
Sub Main()
' Create an isntance of XmlTextReader and call Read method to read the(file)
Dim textReader As XmlTextReader = New XmlTextReader("C:\\books.xml")
textReader.Read()
' If the node has value
If textReader.HasValue Then
' Move to fist element
textReader.MoveToElement()
Console.WriteLine("XmlTextReader Properties Test")
Console.WriteLine("===================")
' Read this element's properties and display them on console
Console.WriteLine("Name:" + textReader.Name)
Console.WriteLine("Base URI:" + textReader.BaseURI)
Console.WriteLine("Local Name:" + textReader.LocalName)
Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString())
Console.WriteLine("Depth:" + textReader.Depth.ToString())
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString())
Console.WriteLine("Node Type:" + textReader.NodeType.ToString())
Console.WriteLine("Attribute Count:" + textReader.Value.ToString())
End If
End Sub
End
Module

The NodeType property of XmlTextReader is important when you want to know the content type of a document. The XmlNodeType enumeration has a member for each type of XML item such as Attribute, CDATA, Element, Comment, Document, DocumentType, Entity, ProcessInstruction, WhiteSpace and so on.

List 2 code sample reads an XML document, finds a node type and writes information at the end with how many node types a document has. 

List 2. Retrieving NodeType Information.

' Import System.Xml namespace
Imports System.Xml
Module Module1
Sub Main()
Dim ws, pi, dc, cc, ac, et, el, xd As Integer
' Read a document
Dim textReader As XmlTextReader = New XmlTextReader("C:\\books.xml")
' Read until end of file
While textReader.Read()
Dim nType As XmlNodeType = textReader.NodeType
' If node type us a declaration
If nType = XmlNodeType.XmlDeclaration Then
Console.WriteLine("Declaration:" + textReader.Name.ToString())
xd = xd + 1
End If
' if node type is a comment
If nType = XmlNodeType.Comment Then
Console.WriteLine("Comment:" + textReader.Name.ToString())
cc = cc + 1
End If
' if node type us an attribute
If nType = XmlNodeType.Attribute Then
Console.WriteLine("Attribute:" + textReader.Name.ToString())
ac = ac + 1
End If
' if node type is an element
If nType = XmlNodeType.Element Then
Console.WriteLine("Element:" + textReader.Name.ToString())
el = el + 1
End If
' if node type is an entity
If nType = XmlNodeType.Entity Then
Console.WriteLine("Entity:" + textReader.Name.ToString())
et = et + 1
End If
' if node type is a Process Instruction
If nType = XmlNodeType.Entity Then
Console.WriteLine("Entity:" + textReader.Name.ToString())
pi = pi + 1
End If
' if node type a document
If nType = XmlNodeType.DocumentType Then
Console.WriteLine("Document:" + textReader.Name.ToString())
dc = dc + 1
End If
' if node type is white space
If nType = XmlNodeType.Whitespace Then
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString())
ws = ws + 1
End If
End While
Console.WriteLine("Total Comments:" + cc.ToString())
Console.WriteLine("Total Attributes:" + ac.ToString())
Console.WriteLine("Total Elements:" + el.ToString())
Console.WriteLine("Total Entity:" + et.ToString())
Console.WriteLine("Total Process Instructions:" + pi.ToString())
Console.WriteLine("Total Declaration:" + xd.ToString())
Console.WriteLine("Total DocumentType:" + dc.ToString())
Console.WriteLine("Total WhiteSpaces:" + ws.ToString())
End Sub
End
Module

Writing XML Documents.

The XmlWriter class contains the functionality to write to XML documents. It is an abstract base class used through XmlTextWriter and XmlNodeWriter classes. It contains methods and properties to write to XML documents. This class has many Writexxx method to write every type of item of an XML document. For example, WriteNode, WriteString, WriteAttributes, WriteStartElement, and WriteEndElement are some of them. Some of these methods are used in a start and end pair. For example, to write an element, you need to call WriteStartElement then write a string followed by WriteEndElement.      

Besides many methods, this class has three properties. WriteState, XmlLang, and XmlSpace. The WriteState gets and sets the state of the XmlWriter class. 

Although it's not possible to describe all the Writexxx methods here, let's see some of them here.

First thing we need to do is create an instance of XmlTextWriter using its constructor. XmlTextWriter has three overloaded constructors, which can take a string, stream, or a TextWriter as an argument. We'll pass a string (file name) as an argument, which we're going to create. In my sample example, I create a file myXmlFile.xml in C:\\ dir.

' Create a new file in C:\\ dir
Dim textWriter As XmlTextWriter = New XmlTextWriter("C:\\myXmFile.xml",Nothing)

After creating an instance, first thing you call us WriterStartDocument. When you're done writing, you call WriteEndDocument and TextWriter's Close method. 

textWriter.WriteStartDocument()
............
textWriter.WriteEndDocument()
textWriter.Close() 

The WriteStartDocument and WriteEndDocument methods open and close a document for writing. You must have to open a document before start writing to it.  WriteComment method writes comment to a document. It takes only one string type of argument. WriteString method writes a string to a document. With the help of WriteString, WriteStartElement and WriteEndElement methods pair can be used to write an element to a document. The WriteStartAttribute and WriteEndAttribute pair writes an attribute.

WriteNode is more write method, which writes an XmlReader to a document as a node of the document. For example, you can use WriteProcessingInstruction and WriteDocType methods to write a ProcessingInstruction and DocType items of a document.

'Write the ProcessingInstruction node
Dim PI As String = "type='text/xsl' href='book.xsl'"
textWriter.WriteProcessingInstruction("xml-stylesheet", PI)
'Write the DocumentType node
textWriter.WriteDocType("book", Nothing, Nothing, "<!ENTITY h'softcover'>")
 

The below sample example summarizes all these methods and creates a new xml document with some items in it such as elements, attributes, strings, comments and so on. See Listing 5-14. In this sample example, we create a new xml file c:\xmlWriterText.xml. In this sample example, We create a new xml file c:\xmlWriterTest.xml using XmlTextWriter: 

After that we add comments and elements to the document using Writexxx methods. After that we read our books.xml xml file using XmlTextReader and add its elements to xmlWriterTest.xml using XmlTextWriter. 

List 3. Writing an XML document using XmlTextWriter.

' Import System.Xml namespace
Imports System.Xml
Module Module1
Sub Main()
' Create a new file in C:\\ dir
Dim textWriter As XmlTextWriter = New XmlTextWriter("C:\\myXmFile.xml", Nothing)
' Opens the document
textWriter.WriteStartDocument()
' Write comments
textWriter.WriteComment("First Comment XmlTextWriter Sample Example")
textWriter.WriteComment("myXmlFile.xml in root dir")
' Write first element
textWriter.WriteStartElement("Student")
textWriter.WriteStartElement("r", "RECORD", "urn:record")
' Write next element
textWriter.WriteStartElement("Name", "")
textWriter.WriteString("Student")
textWriter.WriteEndElement()
' Write one more element
textWriter.WriteStartElement("Address", "")
textWriter.WriteString("Colony")
textWriter.WriteEndElement()
' WriteChars
textWriter.WriteStartElement("Char")
Dim ch() As Char = {"b", "l", "last"}
textWriter.WriteChars(ch, 0, ch.Length)
textWriter.WriteEndElement()
' Ends the document.
textWriter.WriteEndDocument()
' close writer
textWriter.Close()
End Sub
End
Module

Using XmlDocument.

The XmlDocument class represents an XML document. This class provides similar methods and properties we've discussed earlier in this article. 

Load and LoadXml are two useful methods of this class. A Load method loads XML data from a string, stream, TextReader or XmlReader. LoadXml method loads XML document from a specified string. Another useful method of this class is Save. Using Save method you can write XML data to a string, stream, TextWriter or XmlWriter. The following example in List 4 shows you how to use XmlDocument class's Read, ReadXml and Save methods. 

'Create the XmlDocument.
Dim doc As New XmlDocument
doc.LoadXml(("<Student type='regular' Section='B'><Name>TommyLex</Name></Student>"))
'Save the document to a file.
doc.Save("C:\\std.xml")
 

You can also use Save method to display contents on console if you pass Console.Out as a parameter. For example: 

  doc.Save(Console.Out) 

Here is one example of how to load an XML document using XmlTextReader. You read a file in an XmlTextReader and pass this in XmlDocument's Load method to load the document.

Dim doc As New XmlDocument
'Load the the document with the last book node.
Dim reader As New XmlTextReader("c:\\books.xml")
reader.Read()
doc.Load(reader)
doc.Save(Console.Out)
 

XmlDataDocument and DataSet.

The XmlDataDocument is used to provide synchronization between DataSet and XML documents. You can use XmlDataDocument to read an XML document and generate a DataSet or read data from a DataSet and generate an XML file.

Coming in Part 2...


Login to add your contents and source code to this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Become a Sponsor
 Comments
to know splitting xml file by prasath On April 4, 2006

it is very easy...

Reply | Email | Delete | Modify | 
to develop login system in vb.net and using Xml database. by binod On November 24, 2007
please suggest as quickly as possible
Reply | Email | Delete | Modify | 
xml conversion by sundharvijay On November 13, 2009
hi,
i need ms word to xml converter source code... could u help me..
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.