Programmatically Creating an XmlDataSource Control : XmlDataSource « XML « ASP.Net






Programmatically Creating an XmlDataSource Control

<%@ Page Language="C#" %>

<script runat="server">    
protected void Page_Load(object sender, EventArgs e)
{
    XmlDataSource bookSource = new XmlDataSource();
    bookSource.DataFile = "~/Data.xml";
    bookSource.XPath = "Data/genre[@name ='Fiction']/book";
    bookView.DataSource = bookSource;
    bookView.DataBind();
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Programmatically Creating an XmlDataSource Control</title>
</head>
<body>    
  <form id="form1" runat="server">
    <div>
        <asp:GridView ID="bookView" Runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField HeaderText="ISBN" DataField="ISBN" SortExpression="ISBN"></asp:BoundField>
                <asp:BoundField HeaderText="Title" DataField="Title" SortExpression="Title"></asp:BoundField>
                <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price"></asp:BoundField>
            </Columns>
        </asp:GridView>         
    </div>
  </form>
</body>
</html>
File: ~/Data.xml

<Data>
  <genre name="Fiction">
    <book ISBN="1" Title="title 1" Price="19.99" Discount="1.999">
      <chapter num="1" name="Introduction">
        Abstract...
      </chapter>
      <chapter num="2" name="Body">
        Abstract...
      </chapter>
      <chapter num="3" name="Conclusion">
        Abstract...
      </chapter>
    </book>
  </genre>
  <genre name="NonFiction">
    <book ISBN="2" Title="title 2" Price="27.95" Discount="2.795">
      <chapter num="1" name="Introduction">
        Abstract...
      </chapter>
      <chapter num="2" name="Body">
        Abstract...
      </chapter>
      <chapter num="3" name="Conclusion">
        Abstract...
      </chapter>
    </book>
  </genre>
</Data>

 








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.Binding XML Data from Other Sources
7.Updating Data through 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