ASP.Net/XML/XmlDataSource — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:30, 26 мая 2010

asp:Xml datasource

<!-- File: XmlAdPanel.aspx -->
<%@ Page language="c#" %>
<HTML>
<body>
<form runat="server">
<asp:Xml id=_xml1 runat="server" 
         DocumentSource="sample.xml"  
         TransformSource="sampleTransform.xsl">
</asp:Xml><br/>
<asp:Panel id=_p1 runat=server HorizontalAlign="center"
                  Visible="true" bgColor="cornsilk">
  <asp:Label id=_l1 runat=server>Panel label</asp:Label>
  <br/>
  <asp:TextBox id=_tb1 runat=server/>
  <br/>
  <asp:Button Text="Push me!" runat=server/>
</asp:Panel> 
</FORM>
  
  </body>
</HTML>

<%--
<items>
  <item>item 1</item>
  <item>item 2</item>
  <item>item 3</item>
  <item>item 4</item>
</items>
--%>
<%--
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               version="1.0">
                
  <xsl:output method="html" indent="yes"/>
<xsl:template match="item">
  <li>
    <xsl:apply-templates />
  </li>
</xsl:template>
<xsl:template match="items">
  <ul>
    <xsl:apply-templates />
  </ul>
</xsl:template>
<!-- Root template match -->  
<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>
</xsl:transform>

--%>



Binding XML Data from an XmlDataSource Control

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Binding XML Data from an XmlDataSource Control</title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="bookView" Runat="server" DataSourceID="bookSource">
            <DataBindings>
                <asp:TreeNodeBinding ImageUrl="~/Images/openbook.gif" TextField="Title" DataMember="book"></asp:TreeNodeBinding>
                <asp:TreeNodeBinding ImageUrl="~/Images/notepad.gif" TextField="name" DataMember="chapter"></asp:TreeNodeBinding>
            </DataBindings>
        </asp:TreeView>        
        <asp:XmlDataSource ID="bookSource" Runat="server" DataFile="~/Data.xml"
            XPath="Data/genre[@name="Fiction"]/book">
        </asp:XmlDataSource>   
    </div>         
    </form>
    XPath="Data/genre[@name="Fiction"]/book"</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>



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>



Caching XML Data in an XmlDataSource Control

       
<%--
Code Revised from
       
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam 
# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>         
       
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        lblCurrentTime.Text = "Current Time is : " + DateTime.Now.ToLongTimeString();    
    }   
   
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Caching XML Data in an XmlDataSource Control</title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>            
        <asp:Label Runat="server" ID="lblCurrentTime"></asp:Label>           
        <asp:GridView ID="GridView1" Runat="server" DataSourceID="XmlDataSource1" 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>        
        <asp:XmlDataSource EnableCaching="true" CacheDuration="100" CacheExpirationPolicy="Absolute"
        ID="XmlDataSource1" Runat="server" DataFile="Bookstore.xml"
            XPath="bookstore/genre[@name="Fiction"]/book">
        </asp:XmlDataSource>
    </div>
    </form>
</body>
</html>


<%--
<bookstore>
  <genre name="Fiction">
    <book ISBN="10-861003-324" Title="title 1" Price="19.99">
      <chapter num="1" name="Introduction">
        A
      </chapter>
      <chapter num="2" name="Body">
        B
      </chapter>
      <chapter num="3" name="Conclusion">
        C
      </chapter>
    </book>
    <book ISBN="1-861001-57-5" Title="title " Price="24.95">
      <chapter num="1" name="Introduction">
        D
      </chapter>
      <chapter num="2" name="Body">
        E
      </chapter>
      <chapter num="3" name="Conclusion">
        F
      </chapter>
    </book>   
  </genre>
  <genre name="NonFiction">
    <book ISBN="10-861003-324" Title="title 2" Price="19.99">
      <chapter num="1" name="Introduction">
        G
      </chapter>
      <chapter num="2" name="Body">
        H
      </chapter>
      <chapter num="3" name="Conclusion">
        I
      </chapter>
    </book>   
    <book ISBN="1-861001-57-6" Title="title 3" Price="27.95">
      <chapter num="1" name="Introduction">
        J
      </chapter>
      <chapter num="2" name="Body">
        K
      </chapter>
      <chapter num="3" name="Conclusion">
        L
      </chapter>
    </book>
  </genre>
</bookstore>
--%>



Create asp XmlDataSource

<%--
Code Revised from
       
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam 
# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>       
                   
       
       
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Binding XML Data from an XmlDataSource Control</title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1">
            <DataBindings>
                <asp:TreeNodeBinding ImageUrl="openbook.gif" TextField="Title" DataMember="book"></asp:TreeNodeBinding>
                <asp:TreeNodeBinding ImageUrl="notepad.gif" TextField="name" DataMember="chapter"></asp:TreeNodeBinding>
            </DataBindings>
        </asp:TreeView>    
    </div>
        <div>
            <asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="Bookstore.xml"
                XPath="bookstore/genre[@name="Fiction"]/book">
            </asp:XmlDataSource>
        </div>
    </form>
XPath="bookstore/genre[@name="Fiction"]/book"</body>
</html>

<%-- Bookstore.xml
<bookstore>
  <genre name="Fiction">
    <book ISBN="10-861003-324" Title="title 1" Price="19.99">
      <chapter num="1" name="Introduction">
        A
      </chapter>
      <chapter num="2" name="Body">
        B
      </chapter>
      <chapter num="3" name="Conclusion">
        C
      </chapter>
    </book>
    <book ISBN="1-861001-57-5" Title="title " Price="24.95">
      <chapter num="1" name="Introduction">
        D
      </chapter>
      <chapter num="2" name="Body">
        E
      </chapter>
      <chapter num="3" name="Conclusion">
        F
      </chapter>
    </book>   
  </genre>
  <genre name="NonFiction">
    <book ISBN="10-861003-324" Title="title 2" Price="19.99">
      <chapter num="1" name="Introduction">
        G
      </chapter>
      <chapter num="2" name="Body">
        H
      </chapter>
      <chapter num="3" name="Conclusion">
        I
      </chapter>
    </book>   
    <book ISBN="1-861001-57-6" Title="title 3" Price="27.95">
      <chapter num="1" name="Introduction">
        J
      </chapter>
      <chapter num="2" name="Body">
        K
      </chapter>
      <chapter num="3" name="Conclusion">
        L
      </chapter>
    </book>
  </genre>
</bookstore>
--%>



Enter an XML filename or raw XML starting with

<%@ Page Language="C#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>Default</title>
    <script runat="server">
  private void LoadButton_Click(object sender, System.EventArgs e) {
    System.IO.Stream xmlDocStream = GetXmlDoc(XmlSourceTextBox.Text);
    System.Xml.XmlDocument xmlSource = new System.Xml.XmlDocument();
    xmlSource.Load(xmlDocStream);
    ResultText.Text=xmlSource.InnerXml;
  }
  public static System.IO.Stream GetXmlDoc(string xmlsource) {
    System.IO.Stream stream=null;
    if(xmlsource.StartsWith("<?xml") || xmlsource.StartsWith("<schema") ) {
        stream = new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(xmlsource));
    } else {
        try {
          System.Uri xmluri = new System.Uri(xmlsource);          
          if(xmluri.IsFile) {
              stream = new System.IO.FileStream(xmlsource, System.IO.FileMode.Open);
          } else {
              System.Net.HttpWebRequest request =  (System.Net.HttpWebRequest) System.Net.WebRequest.Create(xmluri);
              System.Net.WebResponse response = request.GetResponse();      
              stream = response.GetResponseStream();
          }
        }catch(Exception e) {
        }
    }
    return stream;
  }
    </script>
  </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
      Enter an XML filename or just some raw XML starting with &lt;?xml:<br />
      <asp:textbox id="XmlSourceTextBox" runat="server" Width="377px" Height="162px" TextMode="MultiLine"></asp:textbox><br />
      <asp:button id="LoadButton" runat="server" Text="Load XML Document" OnClick="LoadButton_Click"></asp:button><br />
      <asp:TextBox id="ResultText" runat="server" Width="381px" Height="194px" TextMode="MultiLine"></asp:TextBox>
    </form>
  </body>
</HTML>



Fill XmlDataSource with your code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="XmlDataSourceNoFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:XmlDataSource ID="sourceDVD" runat="server"  ></asp:XmlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sourceDVD">
            <Columns>
                <asp:TemplateField HeaderText="DVD">
                <ItemTemplate>
                <%#XPath("./@ID")%><br />
                  <b><%#XPath("./Title")%></b><br />
                  <%#XPath("./Director")%><br />
                  <%#XPath("./Price", "{0:c}")%><br />
                         </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class XmlDataSourceNoFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     sourceDVD.Data=@"<DvdList>
     <DVD ID="1" Category="category 1">
      <Title>title 1</Title>
      <Director>director 1</Director>
      <Price>1</Price>
      <Starring>
         <Star>star 1</Star>
         <Star>star 2</Star>
      </Starring>
   </DVD>
   </DvdList>";

  }
}



Handling XmlDataSource Events

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<script runat="server">
protected void bookSource_Transforming(object sender, EventArgs e) 
{
  int discountPercentage = 10;  
  XsltArgumentList argList = new XsltArgumentList();
  argList.AddParam("discount", "", discountPercentage.ToString());
  ((XmlDataSource) sender).TransformArgumentList = argList;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Handling XmlDataSource Events</title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="bookView" Runat="server" DataSourceID="bookSource" 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>
                <asp:BoundField HeaderText="Discount" DataField="Discount" SortExpression="Discount"></asp:BoundField>
            </Columns>
        </asp:GridView>        
        <asp:XmlDataSource ID="bookSource" Runat="server" DataFile="~/Data.xml"
            XPath="Data/genre[@name ="Fiction"]/book" TransformFile="~/Data_with_parameter.xsl"
            OnTransforming="bookSource_Transforming">            
        </asp:XmlDataSource>&nbsp;
    </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>
File: ~/Data_with_parameter.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="discount"/>
  <xsl:template match="Data">
    <Data>
      <xsl:apply-templates select="genre"/>
    </Data>
  </xsl:template>
  <xsl:template match="genre">
    <genre>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <xsl:apply-templates select="book"/>
    </genre>
  </xsl:template>
  <xsl:template match="book">
    <book>
      <xsl:attribute name="ISBN">
        <xsl:value-of select="@ISBN"/>
      </xsl:attribute>
      <xsl:attribute name="title">
        <xsl:value-of select="@Title"/>
      </xsl:attribute>
      <xsl:attribute name="price">
        <xsl:value-of select="@Price"/>
      </xsl:attribute>
      <xsl:attribute name="discount">
        <xsl:value-of select="$discount * @Price"/>
      </xsl:attribute>
      <xsl:apply-templates select="chapters/chapter" />
    </book>
  </xsl:template>
  <xsl:template match="chapter">
    <chapter>
      <xsl:attribute name="num">
        <xsl:value-of select="@num"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </chapter>
  </xsl:template>
</xsl:stylesheet>



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>



Updating Data through XmlDataSource Control

<%@ Page Language="C#" %>
<%@ Import NameSpace="System.Xml" %>
<script runat="server" >
    void btnDiscount_Click(Object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc = (XmlDocument)bookSource.GetXmlDocument();
        double discountPercent = Convert.ToInt32(txtDiscountPercent.Text);
        string path = "Data/genre/book";
        XmlNodeList nodeList = doc.SelectNodes(path);
        for(int i=0; i< nodeList.Count ; i++)
        {
            XmlNode node = nodeList[i];
            double price = Convert.ToDouble(node.Attributes["Price"].Value);
            double discount = price * (discountPercent/100);
            XmlAttribute discountAttribute = doc.CreateAttribute("Discount");
            discountAttribute.Value = discount.ToString();
            node.Attributes.Append(discountAttribute);            
        }
        bookSource.Save();
        bookRepeater.DataBind();
    }
</script>
<html>
<head>
    <title>Updating Data through XmlDataSource Control</title>
</head>
<body>
<form id="Form1" runat="server" >
  <asp:XmlDataSource runat="server" ID="bookSource" XPath="Data/genre/book"
    DataFile="~/Data.xml" EnableViewState="True"/>
  <asp:Repeater runat="server" ID="bookRepeater" DataSourceID="bookSource" >
    <ItemTemplate >
        <h2><%# XPath ("@Title") %> </h2>
        <b>ISBN:</b><%# XPath ("@ISBN") %> <%# XPath ("author/last-name/text()") %>        
        <b>Price:</b><%# XPath ("@Price") %>
        <b>Discount:</b><%# XPath ("@Discount") %>
    </ItemTemplate>
  </asp:Repeater>
  <p>
  Enter the discount percentage:<asp:TextBox runat="server" ID="txtDiscountPercent" />
  <asp:Button runat="server" ID="btnAddDiscount" onclick="btnDiscount_Click"
    Text="Add Discount" /></p>
</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>



Use ItemTemplate and XPath to display data from XmlDataSource

<%@ Page Language="C#" AutoEventWireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="sourceDVD" 
                           runat="server" 
                           DataFile="~/Data.xml">
        </asp:XmlDataSource>
        <asp:GridView ID="GridView1" 
                      runat="server" 
                      AutoGenerateColumns="False" 
                      DataSourceID="sourceDVD">
            <Columns>
                <asp:TemplateField HeaderText="DVD">
                <ItemTemplate>
                <%#XPath("./@ID")%><br />
                  <b><%#XPath("./Title")%></b><br />
                  <%#XPath("./Director")%><br />
                  <%#XPath("./Price", "{0:c}")%><br />
                         </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>
File: Data.xml
<?xml version="1.0"?>
<DvdList>
   <DVD ID="1" Category="Category 1">
      <Title>title 1</Title>
      <Director>directory 2</Director>
      <Price>1</Price>
      <Starring>
         <Star>star 1</Star>
         <Star>star 2</Star>
      </Starring>
   </DVD>
   <DVD ID="2" Category="Category 2">
      <Title>title 2</Title>
      <Director>directory 2</Director>
      <Price>2</Price>
      <Starring>
         <Star>star 3</Star>
         <Star>star 4</Star>
      </Starring>
   </DVD>
</DvdList>



Using Inline XML Data in an XmlDataSource Control

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Using Inline XML Data in an XmlDataSource Control</title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="bookView" Runat="server" DataSourceID="bookSource">
            <DataBindings>
                <asp:TreeNodeBinding ImageUrl="~/Images/openbook.gif" TextField="Title" DataMember="book"></asp:TreeNodeBinding>
                <asp:TreeNodeBinding ImageUrl="~/Images/notepad.gif" TextField="name" DataMember="chapter"></asp:TreeNodeBinding>
            </DataBindings>
        </asp:TreeView>    
    </div>
        <div>
            <asp:XmlDataSource ID="bookSource" Runat="server" XPath="Data/book">
                                 
<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>

                </Data>
            </asp:XmlDataSource>
        </div>
    </form>
</html>



Using the XmlDataSource control to consume an RSS feed

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" %>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="XmlDataSource1" 
                           Runat="server"
                           DataFile="http://msdn.microsoft.ru/rss.xml"
                           XPath="rss/channel/item">
        </asp:XmlDataSource>
    </div>
    </form>
</body>
</html>



XmlDataSource and XML document

<%@ Page Language="C#" AutoEventWireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="sourceDVD" 
                           runat="server" 
                           DataFile="~/Data.xml">
        </asp:XmlDataSource>
        <asp:GridView ID="GridView1" 
                      runat="server" 
                      AutoGenerateColumns="False" 
                      DataSourceID="sourceDVD">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>
File: Data.xml
<?xml version="1.0"?>
<DvdList>
   <DVD ID="1" Category="Category 1">
      <Title>title 1</Title>
      <Director>directory 2</Director>
      <Price>1</Price>
      <Starring>
         <Star>star 1</Star>
         <Star>star 2</Star>
      </Starring>
   </DVD>
   <DVD ID="2" Category="Category 2">
      <Title>title 2</Title>
      <Director>directory 2</Director>
      <Price>2</Price>
      <Starring>
         <Star>star 3</Star>
         <Star>star 4</Star>
      </Starring>
   </DVD>
</DvdList>