ASP.Net/XML/XmlDataSource — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:51, 26 мая 2010
Содержание
- 1 asp:Xml datasource
- 2 Binding XML Data from an XmlDataSource Control
- 3 Binding XML Data from Other Sources
- 4 Caching XML Data in an XmlDataSource Control
- 5 Create asp XmlDataSource
- 6 Enter an XML filename or raw XML starting with
- 7 Fill XmlDataSource with your code
- 8 Handling XmlDataSource Events
- 9 Programmatically Creating an XmlDataSource Control
- 10 Updating Data through XmlDataSource Control
- 11 <%# XPath ("@Title") %>
- 12 Use ItemTemplate and XPath to display data from XmlDataSource
- 13 Using Inline XML Data in an XmlDataSource Control
- 14 Using the XmlDataSource control to consume an RSS feed
- 15 XmlDataSource and XML document
asp:Xml datasource
<source lang="csharp">
<%@ Page language="c#" %> <HTML> <body> <form runat="server"> <asp:Xml id=_xml1 runat="server"
DocumentSource="sample.xml" TransformSource="sampleTransform.xsl">
</asp:Xml>
<asp:Panel id=_p1 runat=server HorizontalAlign="center"
Visible="true" bgColor="cornsilk"> <asp:Label id=_l1 runat=server>Panel label</asp:Label>
<asp:TextBox id=_tb1 runat=server/>
<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">
</xsl:template> <xsl:template match="items">
-
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template> </xsl:transform>
--%>
</source>
Binding XML Data from an XmlDataSource Control
<source lang="csharp">
<%@ 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">
<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>
</form> XPath="Data/genre[@name="Fiction"]/book"</body>
</html> File: ~/Data.xml
<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>
</source>
Binding XML Data from Other Sources
<source lang="csharp">
<%@ 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>
</source>
Caching XML Data in an XmlDataSource Control
<source lang="csharp">
<%-- 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">
<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>
</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> --%>
</source>
Create asp XmlDataSource
<source lang="csharp">
<%-- 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">
<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>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="Bookstore.xml" XPath="bookstore/genre[@name="Fiction"]/book"> </asp:XmlDataSource>
</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> --%>
</source>
Enter an XML filename or raw XML starting with
<source lang="csharp">
<%@ 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 <?xml:
<asp:textbox id="XmlSourceTextBox" runat="server" Width="377px" Height="162px" TextMode="MultiLine"></asp:textbox>
<asp:button id="LoadButton" runat="server" Text="Load XML Document" OnClick="LoadButton_Click"></asp:button>
<asp:TextBox id="ResultText" runat="server" Width="381px" Height="194px" TextMode="MultiLine"></asp:TextBox> </form> </body>
</HTML>
</source>
Fill XmlDataSource with your code
<source lang="csharp">
<%@ 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">
<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")%>
<%#XPath("./Title")%>
<%#XPath("./Director")%>
<%#XPath("./Price", "{0:c}")%>
</ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
</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>";
}
}
</source>
Handling XmlDataSource Events
<source lang="csharp">
<%@ 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">
<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>
</form>
</body> </html> File: ~/Data.xml
<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>
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"> <xsl:apply-templates select="genre"/> </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>
</source>
Programmatically Creating an XmlDataSource Control
<source lang="csharp">
<%@ 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">
<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>
</form>
</body> </html> File: ~/Data.xml
<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>
</source>
Updating Data through XmlDataSource Control
<source lang="csharp">
<%@ 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 >
<%# XPath ("@Title") %>
ISBN:<%# XPath ("@ISBN") %> <%# XPath ("author/last-name/text()") %> Price:<%# XPath ("@Price") %> Discount:<%# XPath ("@Discount") %> </ItemTemplate> </asp:Repeater>
Enter the discount percentage:<asp:TextBox runat="server" ID="txtDiscountPercent" /> <asp:Button runat="server" ID="btnAddDiscount" onclick="btnDiscount_Click" Text="Add Discount" />
</form> </body> </html> File: ~/Data.xml
<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>
</source>
Use ItemTemplate and XPath to display data from XmlDataSource
<source lang="csharp">
<%@ 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">
<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")%>
<%#XPath("./Title")%>
<%#XPath("./Director")%>
<%#XPath("./Price", "{0:c}")%>
</ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
</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>
</source>
Using Inline XML Data in an XmlDataSource Control
<source lang="csharp">
<%@ 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">
<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" XPath="Data/book">
<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>
</asp:XmlDataSource>
</form>
</html>
</source>
Using the XmlDataSource control to consume an RSS feed
<source lang="csharp">
<!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">
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="http://msdn.microsoft.ru/rss.xml" XPath="rss/channel/item"> </asp:XmlDataSource>
</form>
</body> </html>
</source>
XmlDataSource and XML document
<source lang="csharp">
<%@ 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">
<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>
</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>
</source>