place.xml
-----------
<?xml version="1.0" encoding="utf-8" ?>
<Places>
<Place>
<Id> 1 </Id>
<City>Chennai</City>
<Country>INDIA </Country>
</Place>
<Place>
<Id> 2 </Id>
<City> Atlanta </City>
<Country>USA </Country>
</Place>
</Places>
Default.aspx
----------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="XML_Default8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Reading XML</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl" runat="server"></asp:DropDownList> <br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
<asp:GridView ID="grxml" runat="server">
</asp:GridView>
</form>
</body>
</html>
Default.aspx.cs
----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using System.Xml;
using System.Text;
public partial class XML_Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/place.xml"));
XmlNodeList bookList = doc.GetElementsByTagName("Place");
foreach (XmlNode node in bookList)
{
XmlElement bookElement = (XmlElement)node;
string ctry = bookElement.GetElementsByTagName("Country")[0].InnerText;
ddl.Items.Add(ctry);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("Id");
DataColumn dc1 = new DataColumn("City");
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
string ct = ddl.SelectedItem.ToString();
string dataPath = Server.MapPath("~/place.xml");
DataSet dSet = new DataSet();
dSet.ReadXml(dataPath);
DataRow[] rows = dSet.Tables[0].Select(" Country = '" + ct + "'");
foreach (DataRow dr in rows)
{
DataRow myRow = dt.NewRow();
myRow["Id"] = dr["Id"];
myRow["City"] = dr["City"];
dt.Rows.Add(myRow);
}
grxml.DataSource = dt;
grxml.DataBind();
}
}
0 comments:
Post a Comment