Wednesday, November 19, 2008

Reading XML file using XmlDocument

Extensible Markup Language or pupularly known XML is one of the latest standard to share data across different platforms. You can write your application in VB.NET under Windows which pass the data to be read by C++ application done in Linux using this method. Does it sounds great?

To make this happen, .NET framework since 1.0 version has introduced several classes and functions to be used for XML. One of the frequently used class is XMLDocument. In this example I'll show how you can read the XML file using XMLDocument class.

Given this xml file called Customer.xml


Customer.xml

Now, we gonna use XmlDocument class to read the file.

Dim xDoc As New XmlDocument
xDoc.Load("C:\Temp\Customer.xml)

Dim nodes As XmlNodeList = xDoc.SelectNodes("Report/Customer")

For Each node As XmlNode In nodes

Dim strFirstName As String = node("FirstName").InnerXml.Trim
Dim strLastName As String = node("LastName").InnerXml.Trim
MsgBox("First Name:" & strFirstName)
MsgBox("Last Name:" & strLastName)

Next



Actually there are several ways to read the xml file, but for me, XmlDocument method is still the best way that always satisfy me.

No comments: