Below is the sample code to parse the XML Document
XMLFile5.xml
<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<student>
<name>Test1</name>
<Id>1</Id>
</student>
<student>
<name>Test2</name>
<Id>2</Id>
</student>
</StudentList>
ExXmlParser.aspx.cs
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 + " ");
}
Response.Write("<br/>");
}
}
}
XMLFile5.xml
<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<student>
<name>Test1</name>
<Id>1</Id>
</student>
<student>
<name>Test2</name>
<Id>2</Id>
</student>
</StudentList>
ExXmlParser.aspx.cs
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 + " ");
}
Response.Write("<br/>");
}
}
}
Below is the o/p
No comments:
Post a Comment