ASP.Net XmlDataSource Example, How to use <asp:XmlDataSource> Control in ASP.NET C#

<asp:XmlDataSource
CacheDuration="string|Infinite"
CacheExpirationPolicy="Absolute|Sliding"
CacheKeyDependency="string"
DataFile="string"
EnableCaching="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnTransforming="Transforming event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
TransformArgumentList="string"
TransformFile="string"
Visible="True|False"
XPath="string"
>
<Data>string</Data>
<Transform>string</Transform>
</asp:XmlDataSource>

The XmlDataSource typically loads XML data from an XML file, which is specified by the DataFile property. XML data can also be stored directly by the data source control in string form using the Data property. If you want to transform the XML data before it is displayed by a data-bound control, you can provide an Extensible Stylesheet Language (XSL) style sheet for the transformation. As with the XML data, you typically load the style sheet from a file, indicated by the TransformFile property, but you can also store it in string form directly using the Transform property.

The XmlDataSource control is a data source control that presents XML data to data-bound controls. The XmlDataSource control can be used by data-bound controls to display both hierarchical and tabular data. The XmlDataSource control is typically used to display hierarchical XML data in read-only scenarios. Because the XmlDataSource control extends the HierarchicalDataSourceControl class, it works with hierarchical data. The XmlDataSource control also implements the IDataSource interface and works with tabular, or list-style, data.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XmlDataSource.aspx.cs" Inherits="XmlDataSource" %>

<!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>ASP.Net XmlDataSource: How to use</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">XmlDataSource</h2>
<asp:CheckBoxList
ID="CheckBoxListExample"
runat="server"
>
</asp:CheckBoxList>
<asp:XmlDataSource
ID="XmlDataSourceExample"
runat="server"
>
<Data>
<Players>
<Player ID="1" Name="John" />
<Player ID="2" Name="Jim" />
<Player ID="3" Name="Jones" />
<Player ID="4" Name="Jenny" />
<Player ID="5" Name="Ben" />
</Players>
</Data>
</asp:XmlDataSource>
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class XmlDataSource : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
CheckBoxListExample.DataSourceID = "XmlDataSourceExample";
CheckBoxListExample.DataTextField = "Name";
CheckBoxListExample.DataValueField = "ID";
CheckBoxListExample.RepeatColumns = 3;
CheckBoxListExample.BorderWidth = 2;
CheckBoxListExample.BorderColor = System.Drawing.Color.DarkGreen;
CheckBoxListExample.BackColor = System.Drawing.Color.Green;
CheckBoxListExample.ForeColor = System.Drawing.Color.White;
}
}
}
Tags: ,
Hot on Web:


About author