Binding XML Data from Other Sources : XmlDataSource « XML « ASP.Net






Binding XML Data from Other Sources

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">    
protected void Page_Load(object sender, EventArgs e)
{
    string connectionString = WebConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        string sql = "Select ProductID, Name from Production.Product AS Product " +
            "Order by ProductID FOR XML AUTO, ROOT('Products')";
        SqlCommand command = new SqlCommand(sql, connection);
        XmlReader reader = command.ExecuteXmlReader();        
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        productsSource.Data = doc.OuterXml;
        productsSource.XPath = "Products/Product";
    }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Binding XML Data from Other Sources</title>
</head>
<body>    
  <form id="form1" runat="server">
    <asp:XmlDataSource id="productsSource" runat="server"/>
     <asp:GridView ID="productView" Runat="server" DataSourceID="productsSource" 
        AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField HeaderText="Product ID" DataField="ProductID"></asp:BoundField>
            <asp:BoundField HeaderText="Product Name" DataField="Name"></asp:BoundField>                
        </Columns>
     </asp:GridView>    
  </form>
</body>
</html>

 








Related examples in the same category

1.asp:Xml datasource
2.Caching XML Data in an XmlDataSource Control
3.Create asp XmlDataSource
4.Binding XML Data from an XmlDataSource Control
5.Using Inline XML Data in an XmlDataSource Control
6.Updating Data through XmlDataSource Control
7.Programmatically Creating an XmlDataSource Control
8.Handling XmlDataSource Events
9.Fill XmlDataSource with your code
10.XmlDataSource and XML document
11.Use ItemTemplate and XPath to display data from XmlDataSource
12.Using the XmlDataSource control to consume an RSS feed
13.Enter an XML filename or raw XML starting with