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

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

Текущая версия на 11:57, 26 мая 2010

Create XmlDataSource

<%@ Page Language="C#" AutoEventWireup="true" %>
<!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>XML Data Binding</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:XmlDataSource ID="sourceXml" 
                       runat="server"
                       DataFile="data.xml" />
    <asp:GridView ID="GridView1" 
                  runat="server" 
                  AutoGenerateColumns="True"
                  DataSourceID="sourceXml" 
                  Visible="False"/>
    <br />
    <asp:TreeView ID="TreeView1" 
                  runat="server" 
                  DataSourceID="sourceXml">
            <DataBindings>
                <asp:TreeNodeBinding DataMember="SuperProProductList" Text="Product List" />
                <asp:TreeNodeBinding DataMember="Category"  TextField="Name" />
                <asp:TreeNodeBinding DataMember="Product" TextField="Name" />
                <asp:TreeNodeBinding DataMember="Price" TextField="#InnerText" />
            </DataBindings>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>
File: data.xml
<?xml version="1.0" standalone="yes"?>
<SuperProProductList xmlns="SuperProProductList" >
  <Category Name="Hardware">
    <Product ID="1" Name="Chair">
      <Price>49.33</Price>
    </Product>
    <Product ID="2" Name="Car">
      <Price>43398.55</Price>
    </Product>
  </Category>
  <Category Name="Produce">
    <Product ID="3" Name="PC">
      <Price>49.99</Price>
    </Product>
  </Category>
</SuperProProductList>


Displaying an XML document with the Menu control.

File: Data.xml
<menu>
  <category text="appetizer">
    <item text="soup" price="12.56" />
    <item text="cheese" price="17.23" />
  </category>
  <category text="entree">
    <item text="duck" price="89.21" />
    <item text="chicken" price="34.56" />
  </category>
  <category text="dessert">
    <item text="cake" price="23.43" />
    <item text="pie" price="115.46" />
  </category>
</menu>
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>Menu XML Complex</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Menu
        id="Menu1"
        DataSourceID="srcMenu"
        Runat="server">
        <DataBindings>
        <asp:MenuItemBinding
            DataMember="category"
            TextField="text" />
        <asp:MenuItemBinding
            DataMember="item"
            TextField="text"
            ValueField="price" />
        </DataBindings>
    </asp:Menu>
    <asp:XmlDataSource
        id="srcMenu"
        DataFile="Data.xml"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Transform XmlDataSource with XSLT

<%@ 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"
                        TransformFile="Data.xsl"/>
     <asp:TreeView ID="TreeView1" 
                   runat="server" 
                   DataSourceID="sourceDVD"
                   AutoGenerateDataBindings="False">
            <DataBindings>
                <asp:TreeNodeBinding DataMember="Movies" Text="Movies" />
                <asp:TreeNodeBinding DataMember="DVD" TextField="Title" />
                <asp:TreeNodeBinding DataMember="Stars" TextField="Name" />
            </DataBindings>
     </asp:TreeView>
    </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>
File: Data.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <xsl:element name="Movies">
      <xsl:apply-templates select="//DVD" />
    </xsl:element>
  </xsl:template>
  <xsl:template match="DVD">
    <xsl:element name="{name()}">
      <xsl:attribute name="ID">
        <xsl:value-of select="@ID"/>
      </xsl:attribute>
      <xsl:attribute name="Title">
        <xsl:value-of select="Title/text()"/>
      </xsl:attribute>
      <xsl:apply-templates select="Starring" />
    </xsl:element>
  </xsl:template>
  <xsl:template match="Starring">
    <xsl:element name="Stars">
      <xsl:attribute name="Name">
        <xsl:value-of select="Star/text()"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>


XmlDataSource and XPath

<%@ 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" 
                           XPath="/DvdList/DVD/Starring/Star"/>
           <asp:GridView ID="GridView1" 
                         runat="server" 
                         DataSourceID="sourceDVD" 
                         AutoGenerateColumns="False">
              <Columns>
                <asp:TemplateField HeaderText="DVD">
                  <ItemTemplate>
                    <%# XPath(".")%><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>