XML in ASP.NET

This Post will show some startup on XML file manipulation in ASP.net

Example XML file: bookstore.xml

<?xml version=”1.0″ encoding=”iso-8859-1″?>

<bookstore>

<book category=”COOKING” ID=”1″>

<title lang=”en”>Everyday Italian</title>

<author>Giada De Laurentiis</author>

<year>2005</year>

<price>30.00</price>

</book>

<book category=”CHILDREN” ID=”2″>

<title lang=”en”>Harry Potter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

<book category=”WEB” ID=”3″>

<title lang=”en”>Learning XML</title>

<author>Erik T. Ray</author>

<year>2003</year>

<price>39.95</price>

</book>

</bookstore>

Now Open a VS project and Add an XML file. Save the previous XML as XML file to the project XML file.

Just a sample UI to interact:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”XmlExample1._Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; >

<head runat=”server”>

<title></title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:Button ID=”btnParse” runat=”server” Text=”Parse XMLDoc With DOM Recurcive”

onclick=”btnParse_Click” />

<br />

<hr />

<asp:Label ID=”lblResult” runat=”server” Text=””></asp:Label>

</div>

</form>

</body>

</html>

Code behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Xml.Linq;

namespace XmlExample1

{

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnParse_Click(object sender, EventArgs e)

{

UsingXPTHSelectNode();

}

private void UsingXPTHSelectNode()

{

string s = “”;

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(MapPath(“bookstore.xml”));

XmlNodeList nList;

nList = xmlDoc.SelectNodes(“//bookstore”);

foreach (XmlNode node in nList)

{

s = string.Format(” Type: {0} ;Name: {1}”, node.NodeType, node.Name);

lblResult.Text += s + “<br/>”;

if (node.HasChildNodes)

{

foreach (XmlNode child in node.ChildNodes)

{

s = string.Format(“Type:{0} Name: {1}; InnerText:{2}”, child.NodeType, child.Name, child.InnerText);

if (child.Attributes.Count > 0)

foreach (XmlAttribute att in child.Attributes)

s += string.Format(”  ATT: >> Type:{0}; Name: {1}; Inner Text:{2}”, att.NodeType, att.Name, att.Value);

lblResult.Text += s + “<br/>”;

}

}

}

}

}

}

Solution structure:

untitled

Output:

XML With XPATH

XML With XPATH


To get a more refine Example:

Simply Call the following function In place of previous function :

private void MoreRefinedUsingXPATHSelectNode()

{

string result = “”;

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(MapPath(“bookstore.xml”));

XmlNode root = xmlDoc.DocumentElement;

result +=root.Name +”<br>” ;

foreach (XmlNode firstChild in root.ChildNodes)

{

result += “Start of Child <br>”;

result += firstChild.Name+ “<br> “;

foreach (XmlAttribute firstChildAtt in firstChild.Attributes)

result += firstChildAtt.Value + “<br> “;

foreach (XmlNode grandChind in firstChild.ChildNodes)

{

result += grandChind.Name + ” : ” + grandChind.InnerText +”<br>”;

}

result += “<br>End of Child<br>”;

}

lblResult.Text = result;

}

Output:

XML Using XPATH Refresh

XML Using XPATH Refresh

Advertisements