Click here to Skip to main content
15,913,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i have a grid view with displaying xml file data. i want to perform filter on gridview using dropdown box .
ex:
i have name ,city,location in xml data
in dropdown i had location
when dropdown change event occures i want to display particular location details on gridview.

my prob is i don't know how to select perticular element value from xml to comapre.
i am very new to xml database.
Posted
Comments
jvamsikrishna 30-May-12 0:58am    
I had achieved to reslove my problem using this code
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;
using System.Drawing;
using System.Data;


public partial class GridViewFilter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlTextReader xtr = new XmlTextReader(Server.MapPath("~/xml/xmltest.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xtr);

GridView1.DataSource = ds;
GridView1.DataBind();
getxml();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();

DropDownList2.DataSource = ds;
DropDownList2.DataTextField =ds.Tables[0].Columns[3].ToString();
DropDownList2.DataValueField = "location";
DropDownList2.DataBind();
}
protected void getxml()
{
XmlTextReader xtr = new XmlTextReader(Server.MapPath("~/xml/xmltest.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xtr);

GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string name= DropDownList1.SelectedItem.Text;
DataTable dt = new DataTable();
dt.Columns.Add("fname", typeof(string));
dt.Columns.Add("lname", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("location", typeof(string));
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("~/xml/xmltest.xml"));
XmlNodeList nodeList = xmldoc.SelectNodes("/karomi/emp");


foreach (XmlNode node in nodeList)
{
if (name== node["name"].InnerText)
{
DataRow dtrow = dt.NewRow();
dtrow["fname"] = node["fname"].InnerText;
dtrow["lname"] = node["lname"].InnerText;
dtrow["name"] = node["name"].InnerText;
dtrow["location"] = node["location"].InnerText;
dt.Rows.Add(dtrow);
}
}

GridView1.DataSource = dt;
GridView1.DataBind();



}
}

 
Share this answer
 
Comments
Prasad_Kulkarni 29-May-12 7:23am    
5'ed
jvamsikrishna 30-May-12 0:54am    
Thanks for you help to reslove my query
Rahul Rajat Singh 30-May-12 0:59am    
you are most welcome. always happy to help.
i give you links i hope it will give you brief idea of xml and solve your query

XML for beginners and experts[^]

best of luck

i am give you simple example i think it will more help you
C#
using System.IO;
using System.Xml;
private void readDatafromXML(string file_Path)
    {
        DataSet DS_purchase;
int timeout = Server.ScriptTimeout;
        Server.ScriptTimeout = int.MaxValue;
        FileInfo FInfo = new FileInfo(file_Path);
        if (FInfo.Exists)
        {
            XmlTextReader xmlReader;
            xmlReader = new XmlTextReader(FInfo.FullName);
            try
            {
                DS_purchase.ReadXml(xmlReader);
                xmlReader.Close();               
            }
            catch (Exception ex)
            {
                xmlReader.Close();
                lbl_err.Text = "Error in Reading XML file " + ex.Message;
            }

            finally
            {
                xmlReader.Close();

            }
        }
        else
            lbl_err.Text = "Session Has been expired.please Try again. ";

// on this dataset you can create your query 
    }
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 29-May-12 7:24am    
Good answer +5!
jvamsikrishna 30-May-12 0:55am    
Your Link has given Detailed view about Xml and thanks for xmlhelper.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900