XmlDataSource filtering and Binding Issue

Last Reply on Sep 09, 2013 10:30 PM By asp.net1

Posted on Sep 08, 2013 11:26 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm8.aspx.cs" Inherits="WebApplication5.NewFolder4.WebForm8" %>

<!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 runat="server">
    <form id="form1" runat="server">
        <asp:XmlDataSource ID="XmlDataSource1" runat="server">
                <emp id="100" empname="Samanta" genid="1" OfficeTime="today" />
                <emp id="200" empname="Lusia" genid="11" OfficeTime="yesterday" />
                <emp id="200" empname="Andria" genid="100" OfficeTime="today" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <asp:TemplateField HeaderText="empname">
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("empname") %>'></asp:Label>




*** You can chnage your Gridview style as you need 


Bind empname only from the XmlDataSource to GridView where Officetime="today"


Output :



output to next line, please use "\n" 

Please do not use LINQ, Generics and XML

You can use Collections and WebControls, HtmlControls 

The code may be look like this
protected void Page_Load(object sender, EventArgs e)
XmlDocument myXml = new XmlDocument();
myXml = (XmlDocument)XmlDataSource1.GetXmlDocument();

XmlReader xtr = new XmlNodeReader(myXml);

DataSet ds = new DataSet();
DataTable dt = new DataTable();

DataRow[] dr;
DataTable dtfilter = new DataTable();

//Here i load all xml data in to dataset
dt = ds.Tables[0];

//Filter here above employe no ten records

dr = dt.Select("OfficeTime = 'Today'");

dtfilter = dr.CopyToDataTable();
GridView1.DataSource = dtfilter;



Posted on Sep 09, 2013 06:00 AM Modified on on Sep 10, 2013 02:05 AM

If you have XML DataSource there is no need of System.XML nor DataSet Directly using XPAth it can be filtered as shown in my article.

 Bind and Filter XmlDataSource Control with GridView in ASP.Net Example

XmlDataSource1.XPath = "/employeeinformationbyday/emp[ @OfficeTime='today' ]";


Posted on Sep 09, 2013 10:30 PM


Pradip says:

Try This




XmlDataSource1.XPath = "/employeeinformationbyday/emp[ @OfficeTime='today' ]";
       GridView1.DataSource = XmlDataSource1;



 Not get anything by this code.  Have you run it ?


protected void Page_Load(object sender, EventArgs e)

XmlDataSource1.XPath = "/employeeinformationbyday/emp[@OfficeTime='today']";
GridView1.DataSource = XmlDataSource1;



thanks for your posting