Reader Level:
ARTICLE

Simple XML Parser in VB.NET

Posted by Shehan Peruma Articles | Visual Basic .NET November 10, 2012
This article shows how to create a very simple XML parser.
  • 0
  • 0
  • 13813
Download Files:
 

This article and the attached source code shows how to parse XML documents using .NET XML class library classes.

The application reads the XML file and then associates the xml tags to variables which are then displayed in a message box.

The namespace System.Xml would be the only additional namespace that we would be using.

Start of by initializing a new instance of the XmlDocument class.

Private xDoc As XmlDocument = New XmlDocument().

Next use the load method to load the XML document from the specified stream.

xDoc.Load("sampleXML.xml").

Use the method GetElementsByTagName() to obtain the addresses of a collection of elements that match the specified name.

Private name As XmlNodeList = xDoc.GetElementsByTagName("myName").
Private age As XmlNodeList = xDoc.GetElementsByTagName("myAge").

XmlNodeList represents an ordered collection of nodes.

Display the results in a message box.

MessageBox.Show("Name: " & name(0).InnerText).

InnerText will return the concatenated values of the node and all its children.

COMMENT USING

Trending up