XmlDataSource.DataFile 屬性

定義

指定資料來源繫結至的 XML 檔案名稱。

public:
 virtual property System::String ^ DataFile { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataFile { get; set; }
member this.DataFile : string with get, set
Public Overridable Property DataFile As String

屬性值

XML 檔案的絕對實體路徑或相對路徑,這個檔案包含 XmlDataSource 控制項所表示的資料。 預設值是 Empty

例外狀況

此文件正在載入。

範例

下列程式碼範例示範如何使用 XmlDataSource 控制項來顯示包含 TreeView 控制項之檔案中的 XML 資料。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:TreeView
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:TreeView>

    </form>
  </body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:TreeView
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:TreeView>

    </form>
  </body>
</html>

程式碼範例中的 XML 檔案具有下列資料:

<books>  
   <computerbooks>  
     <book title="Secrets of Silicon Valley" author="Sheryl Hunter"/>  
     <book title="Straight Talk About Computers" author="Dean Straight"/>  
     <book title="You Can Combat Computer Stress!" author="Marjorie Green"/>                  
   </computerbooks>  
   <cookbooks>  
     <book title="Silicon Valley Gastronomic Treats" author="Innes del Castill"/>  
   </cookbooks>  
</books>  

備註

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

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

適用於

另請參閱