March 20, 2011

XML Parsing in ASP.NET

Below is the sample code to parse the XML Document


<?xml version="1.0" encoding="utf-8" ?>





using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class ExXmlParser : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing

        xdoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/XMLFile5.xml"));//loading XML in xml doc

        XmlNodeList name = xdoc.GetElementsByTagName("student");

        foreach (XmlNode xNode in name)//traversing XML 
            foreach (XmlElement xelement in xNode)

                Response.Write(xelement.FirstChild.Value + "   ");

Below is the o/p 

No comments:

Post a Comment