2013年4月8日 星期一

資料來源控制項(2)

XmlDataSource

The XmlDataSource control provides a means to create a binding connection between controls on your page and an XML file. The XML data source control is best used when you wish to bind to XML data that is represented as hierarchical (such as using in the TreeView control).

  • DataFile :定義要繫結至 XmlDataSource 控制項的 XML 檔案名稱。
  • Data :這個屬性是 XmlDataSource 控制項內部的一個區塊,是用來直接定義 XML 資料用的
  • TransformFile :定義可延伸樣式表語言 (XSL) 檔案 (.xsl) 的名稱。

如果同時設定 DataFileData 屬性,則會以 DataFile 屬性優先,並且會使用 XML 檔案中的資料,而不是在 Data 屬性所指定的 XML 資料。

如果您變更 Data 屬性值,則會引發 DataSourceChanged 事件。如果已啟用快取,並變更 Data 的值,則快取就會失效。

使用 XmlDataSource 將 Xml 資料載入 GridView

載入 XML 資料

GridView 預設支援的 XML 格式如下:

<?xml version="1.0" standalone="yes"?>
<Products>
  <Product Category="Beverages" Name="Chai" QuantityPerUnit="10 boxes x 20 bags" UnitPrice="18.0000" />
  <Product Category="Condiments" Name="Aniseed Syrup" QuantityPerUnit="10 boxes x 20 bags" UnitPrice="10.0000" />
</Products>

如果 XML 資料如上面格式,只要使用 DataFile 屬性指定檔案路徑即可。

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Products1.xml" >
</asp:XmlDataSource>
<asp:GridView ID="GridView4" runat="server" DataSourceID="XmlDataSource1">
</asp:GridView>

轉換 XML 資料

如果 XML 資料如下面格式,則必須藉由 XSLT 檔來轉換格式。

<?xml version="1.0" standalone="yes"?>
<Products>
  <Product>
    <Category>Beverages</Category>
    <Name>Chai</Name>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <UnitPrice>18.0000</UnitPrice>
  </Product>
  <Product>
    <Category>Condiments</Category>
    <Name>Aniseed Syrup</Name>
    <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
    <UnitPrice>10.0000</UnitPrice>
  </Product>
</Products>

XSLT (Extensible Stylesheet Language Transformations) is an XML-based language used for the transformation of XML documents into other XML or “human-readable” documents.

<?xml version="1.0"?>
<xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml"
      omit-xml-declaration="yes"
      indent="yes"
      standalone="yes" />
  <xsl:template match="/">
    <xsl:for-each select="Products">
      <xsl:element name="Products">
        <xsl:for-each select="Product">
          <xsl:element name="Product">
            <xsl:attribute name="Category">
              <xsl:value-of select="Category"/>
            </xsl:attribute>
            <xsl:attribute name="Name">
              <xsl:value-of select="Name"/>
            </xsl:attribute>
            <xsl:attribute name="QuantityPerUnit">
              <xsl:value-of select="QuantityPerUnit"/>
            </xsl:attribute>
            <xsl:attribute name="UnitPrice">
              <xsl:value-of select="UnitPrice"/>
            </xsl:attribute>
          </xsl:element>
        </xsl:for-each>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

要使用這樣格式的 XML 資料,除了使用 DataFile 屬性指定 XML 檔案, 還必須將 XSLT 檔案指定給 TransformFile 屬性才可以。

<asp:XmlDataSource ID="XmlDataSource2" runat="server"
    DataFile="Products2.xml" 
    TransformFile="Products2.xsl" >
</asp:XmlDataSource>
<asp:GridView ID="GridView5" runat="server" DataSourceID="XmlDataSource2">
</asp:GridView>

載入畫面

使用 XmlDataSource 將 Xml 資料載入 TreeView

<?xml version="1.0" standalone="yes"?>
<Products>
  <Product>
    <Category>Beverages</Category>
    <Name>Chai</Name>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <UnitPrice>18.0000</UnitPrice>
  </Product>
  <Product>
    <Category>Condiments</Category>
    <Name>Aniseed Syrup</Name>
    <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
    <UnitPrice>10.0000</UnitPrice>
  </Product>
</Products>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Products">
    <Products>
      <xsl:for-each select="Product">
        <xsl:sort select="Name" order="ascending" />
        <Product>
          <Name>
            <xsl:value-of select="Name"/>
          </Name>
          <Category>
            <xsl:text>Category: </xsl:text>
            <xsl:value-of select="Category"/>
          </Category>
          <QuantityPerUnit>
            <xsl:text>Quantity: </xsl:text>
            <xsl:value-of select="QuantityPerUnit"/>
          </QuantityPerUnit>
          <UnitPrice>
            <xsl:text>Price: </xsl:text>
            <xsl:value-of select="UnitPrice"/>
          </UnitPrice>
        </Product>
      </xsl:for-each>
    </Products>
  </xsl:template>
</xsl:stylesheet>
<asp:XmlDataSource ID="XmlDataSource3" runat="server"
    DataFile="Products3.xml" 
    TransformFile="Products3.xsl" >
</asp:XmlDataSource>
<asp:TreeView id="TreeView1" runat="server" DataSourceID="XmlDataSource3">
    <DataBindings>
        <asp:TreeNodeBinding DataMember="Name" TextField="#InnerText" />
        <asp:TreeNodeBinding DataMember="Category" TextField="#InnerText" />
        <asp:TreeNodeBinding DataMember="QuantityPerUnit" TextField="#InnerText" />
        <asp:TreeNodeBinding DataMember="UnitPrice" TextField="#InnerText" />
    </DataBindings>
</asp:TreeView>

載入畫面

Filtering XML with the XmlDataSource Control

例一

XmlDataSource 的提供 XPath 屬性,可以用來設定資料來源的篩選條件。

針對上面範例,我們可以使用以下的 XPath 表示法來篩選資料。

<asp:XmlDataSource ID="XmlDataSource2" runat="server"
    DataFile="Products2.xml" 
    TransformFile="Products2.xsl" 
    XPath="/Products/Product[@Category='Beverages']" >
</asp:XmlDataSource>

例二

XPath 的使用方法,會因為資料格式的不同而有所不同,底下範例示範如何篩選一個 XML 資料,並繫結到 TreeView 控制項。

<?xml version="1.0" standalone="yes"?>
    <Products>
      <Product>
        <Category>Beverages</Category>
        <Name>Chai</Name>
        <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
        <UnitPrice>18.0000</UnitPrice>
      </Product>
      <Product>
        <Category>Condiments</Category>
        <Name>Aniseed Syrup</Name>
        <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
        <UnitPrice>10.0000</UnitPrice>
      </Product>
      <Product>
        <Category>Condiments</Category>
        <Name>Chef Anton's Cajun Seasoning</Name>
        <QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit>
        <UnitPrice>22.0000</UnitPrice>
      </Product>
      <Product>
        <Category>Produce</Category>
        <Name>Uncle Bob's Organic Dried Pears</Name>
        <QuantityPerUnit>12 - 1 lb pkgs.</QuantityPerUnit>
        <UnitPrice>30.0000</UnitPrice>
      </Product>
      <Product>
        <Category>Beverages</Category>
        <Name>Guaraná Fantástica</Name>
        <QuantityPerUnit>12 - 355 ml cans</QuantityPerUnit>
        <UnitPrice>4.5000</UnitPrice>
      </Product>
      <Product>
        <Category>Beverages</Category>
        <Name>Sasquatch Ale</Name>
        <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
        <UnitPrice>14.0000</UnitPrice>
      </Product>
      <Product>
        <Category>Beverages</Category>
        <Name>Steeleye Stout</Name>
        <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
        <UnitPrice>18.0000</UnitPrice>
      </Product>
    </Products>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Products">
        <Products>
          <xsl:for-each select="Product">
            <xsl:sort select="Name" order="ascending" />
            <Product>
              <Name>
                <xsl:value-of select="Name"/>
              </Name>
              <Category>
                <xsl:text>Category: </xsl:text>
                <xsl:value-of select="Category"/>
              </Category>
              <QuantityPerUnit>
                <xsl:text>Quantity: </xsl:text>
                <xsl:value-of select="QuantityPerUnit"/>
              </QuantityPerUnit>
              <UnitPrice>
                <xsl:text>Price: </xsl:text>
                <xsl:value-of select="UnitPrice"/>
              </UnitPrice>
            </Product>
          </xsl:for-each>
        </Products>
      </xsl:template>
    </xsl:stylesheet>
<asp:XmlDataSource ID="XmlDataSource3" runat="server"
    DataFile="Products3.xml" 
    TransformFile="Products3.xsl"
    XPath="/Products/Product[Category='Category: Beverages']"  >
</asp:XmlDataSource>

NOTE inserting, deleting, modifying, and saving XML data

The XmlDataSource control is typically used when reading XML data. Unlike many of the other data source controls, it does not provide automatic attributes for inserting, deleting, updating, and saving XML data. Instead, you have to write your own custom code if you require this behavior. Please see Chapter 7 for more information on working with XML data in this manner.

SiteMapDataSource

Connecting to Site Navigation Data with SiteMapDataSource

Filtering the Data Shown in the SiteMapDataSource

Sometimes, you might wish to show only a portion of the data in your sitemap data file. The SiteMapDataSource control provides a couple attributes you can use to control the data that is shown.

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
    ShowStartingNode="false" 
    StartingNodeUrl="WebForm2.aspx"
    />
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>

沒有留言:

張貼留言