ARTICLE

Simple XML Parser in C#

Posted by Shehan Peruma Articles | XML April 05, 2005
This article shows how to create a very simple XML parser.
Reader Level:
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.

XmlDocument xDoc = 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.

XmlNodeList name = xDoc.GetElementsByTagName("myName");
XmlNodeList age = 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.

Login to add your contents and source code to this article
post comment
     

DOM reads the all contents of XML markup and builds a tree, so The representation of entire document lies in the memory.
The Xmltextreader is a reader that just read elements without store whas has been read in memory. It just read and parse forward what means you cannot access the attributes read formerly.


Posted by edward Jul 08, 2010

What is the difference between XMLDocument and XMLTextReader?  Thanks.

Danis

Posted by Danis Lui Feb 09, 2010

tank`s

Posted by rahim ismaeeli Jan 19, 2010

rrumesh@gmail.com 

Posted by Rakesh Thakre Sep 26, 2009

Very good fine . It is very useful for me

Posted by vadivel pm Sep 17, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.