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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server">
            <Data>
                <employeeinformationbyday>
                <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" />
                </employeeinformationbyday>
            </Data>
        </asp:XmlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="empname">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("empname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

 

 

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

 

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

 

Output :

Samanta

Andria


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
ds.ReadXml(xtr);
dt = ds.Tables[0];

//Filter here above employe no ten records

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

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



}

 

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

 

.cs

 

1
2
3
XmlDataSource1.XPath = "/employeeinformationbyday/emp[ @OfficeTime='today' ]";
       GridView1.DataSource = XmlDataSource1;
       GridView1.DataBind();

 

 

 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;
GridView1.DataBind();

}

 

thanks for your posting

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html