SIGN UP MEMBER LOGIN:    
ARTICLE

XML Parser in C#

Posted by Sanjay Ahuja Articles | XML in C# April 30, 2002
This project gives you a head start to write XML parser in C#.
Reader Level:
Download Files:
 

This project gives you a head start to write XML parser in C#. The important namespace to achieve our goal would be System.Xml. Though you might find it strange, unlike other languages/class libraries their are no direct functions like startElement and endElement at your rescue. You will have to manage all this on your own. So lets start looking at this important block of the code

void ParseURL(string strUrl)
{
try
{
XmlTextReader reader =
new XmlTextReader(strUrl);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Hashtable attributes =
new Hashtable();
string strURI= reader.NamespaceURI;
string strName= reader.Name;
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
attributes.Add(reader.Name,reader.Value);
}
}
StartElement(strURI,strName,strName,attributes);
break;
//
//you can handle other cases here
//
//case XmlNodeType.EndElement:
// Todo
//case XmlNodeType.Text:
// Todo
default:
break;
}
}
catch (XmlException e)
{
Console.WriteLine("error occured: " + e.Message);
}
}

As we see the main class here is XmlTextReader. XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node. Note that we cache the element name before we move to the attributes.

NodeType property of XmlTextReader  gets the type of the current node. Accordingly we process the node and call necessary functions on it.

You can forward me your comments at lparam@hotmail.com.

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

Thank you  !

Posted by Simple Calm Jan 17, 2010

If you have a schema and wish to parse out XML in a well-structured format, I would suggest using XmlSerializer. See my posting here: http://www.codeproject.com/KB/cs/nicexmlparsing.aspx

Posted by Joseph Armbruster Mar 02, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Nevron Gauge for SharePoint
Become a Sponsor