XmlDataSource.Save 方法

定義

如果 XmlDataSource 屬性已設定,儲存 DataFile 控制項目前存放在記憶體中的 XML 資料到磁碟中。

public:
 void Save();
public void Save ();
member this.Save : unit -> unit
Public Sub Save ()

例外狀況

XML 資料是使用 Data 屬性 (而不是 DataFile 屬性) 載入的。

-或-

指定了 DataFile 屬性的 URL,不過,XmlDataSource 控制項沒有 Web 資源的正確使用權限。

DataFile 屬性指定 URL,然而,它不是 HTTP 的 URL。

-或-

在使用 XmlDataSource 控制項之前,設計工具未正確對應設計階段相對路徑。

DataFile 屬性指定之路徑的存取遭拒絕。

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何使用 XmlDataSource 控制項搭配 TreeView 控制項來顯示及編輯 XML 檔案中包含的 XML 資料。 第二個 XmlDataSource 程式碼範例示範如何使用具有樣板化 Repeater 控制項的控制項來顯示及編輯 XML 檔案中包含的 XML 資料。

下列程式碼範例示範如何使用 XmlDataSource 控制項搭配 TreeView 控制項來顯示及編輯 XML 檔案中包含的 XML 資料。 每次選取 TreeView 節點時, GetXmlDocument 都會使用 方法在記憶體中運算元據,然後儲存至 XML 檔案。 最後, DataBind 會在 控制項上 TreeView 呼叫 ,以重新整理它所顯示的資料。

<%@ Page LANGUAGE="C#" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server" >
  void TreeView1_SelectedNodeChanged(Object sender, EventArgs e)
  {
    XmlDocument myXml = new XmlDocument();
    myXml=(XmlDocument)XmlSource.GetXmlDocument();

    String iterator = TreeView1.SelectedNode.DataPath;

    XmlNode myNode = myXml.SelectSingleNode(iterator);

    myNode.InnerText = "ThisIsATest";
    XmlSource.Save();
    TreeView1.DataBind();
    TreeView1.ExpandAll();
  }
</script>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server">

      <asp:xmldatasource
        runat="server"
        id="XmlSource"
        xpath="/bookstore/book"
        datafile="Booksort.xml"
        enableviewstate="False"
        enablecaching="False" />

      <asp:treeview
        runat="server"
        id="TreeView1"
        ExpandDepth="3"
        datasourceid="XmlSource"
        maxdatabinddepth="3"
        autogeneratedatabindings="False"
        onselectednodechanged="TreeView1_SelectedNodeChanged" >
        <databindings>
          <asp:treenodebinding datamember="book" valuefield="publicationdate" />
          <asp:treenodebinding datamember="title" valuefield="#InnerText" />
          <asp:treenodebinding datamember="author" valuefield="#InnerText" />
          <asp:treenodebinding datamember="first-name" valuefield="#InnerText" />
          <asp:treenodebinding datamember="last-name" valuefield="#InnerText" />
        </databindings>
      </asp:treeview>
    </form>
  </body>
</html>
<%@ Page LANGUAGE="VB" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server" >
  Private Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs)

    Dim myXml As New XmlDocument
    myXml = CType(XmlSource.GetXmlDocument(), XmlDataDocument)

    Dim iterator As String = TreeView1.SelectedNode.DataPath
    Dim myNode As XmlNode = myXml.SelectSingleNode(iterator)

    myNode.InnerText = "ThisIsATest"
    XmlSource.Save()
    TreeView1.DataBind()
    TreeView1.ExpandAll()
  End Sub ' TreeView1_SelectedNodeChanged
</script>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server">

      <asp:xmldatasource
        runat="server"
        id="XmlSource"
        xpath="/bookstore/book"
        datafile="Booksort.xml"
        enableviewstate="False"
        enablecaching="False" />

      <asp:treeview
        runat="server"
        id="TreeView1"
        ExpandDepth="3"
        datasourceid="XmlSource"
        maxdatabinddepth="3"
        autogeneratedatabindings="False"
        onselectednodechanged="TreeView1_SelectedNodeChanged" >
        <databindings>
          <asp:treenodebinding datamember="book" valuefield="publicationdate" />
          <asp:treenodebinding datamember="title" valuefield="#InnerText" />
          <asp:treenodebinding datamember="author" valuefield="#InnerText" />
          <asp:treenodebinding datamember="first-name" valuefield="#InnerText" />
          <asp:treenodebinding datamember="last-name" valuefield="#InnerText" />
        </databindings>
      </asp:treeview>
    </form>
  </body>
</html>

下列程式碼範例示範如何使用 XmlDataSource 控制項搭配樣板化 Repeater 控制項來顯示及編輯 XML 檔案中包含的 XML 資料。 如同先前的範例,使用 方法所 GetXmlDocument 擷取的物件,在記憶體 XmlDataDocument 中運算元據。 最後, DataBind 會在 控制項上 TreeView 呼叫 ,以重新整理它所顯示的資料。

<%@ Page LANGUAGE="C#" SMARTNAVIGATION="false" %>
<%@ Import NameSpace="System.Xml" %>
<script runat="server" >

  void Button1_Click(Object sender, EventArgs e)
  {
    XmlDocument myXml = new XmlDocument();
    myXml=(XmlDocument)XmlSource.GetXmlDocument();

    String path = "bookstore/book/@publicationdate";
    XmlNodeList nodeList;
    nodeList = myXml.SelectNodes(path);
    foreach (XmlNode date in nodeList)
      {
        int helper = int.Parse(date.Value) + 2;
        date.Value = helper.ToString();
      }
    XmlSource.Save();
    Repeater1.DataBind();
  }

</script>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server" >

      <asp:XmlDataSource
        runat="server"
        ID="XmlSource"
        XPath="bookstore/book[@genre='novel']"
        DataFile="Booksort2.xml"
        EnableViewState="True"
        EnableCaching="False" />

      <asp:Repeater
        runat="server"
        ID="Repeater1"
        DataSourceID="XmlSource" >
          <ItemTemplate >
            <h1><%# XPath ("title/text()") %> </h1>
              <b>Author:</b><%# XPath ("author/first-name/text()") %> <%# XPath ("author/last-name/text()") %>
              <b>PublicationDate:</b><%# XPath ("@publicationdate") %>
              <b>Price:</b><%# XPath ("price/text()") %>
          </ItemTemplate>
      </asp:Repeater>


      <p><asp:Button
        runat="server"
        ID="Button1"
        onclick="Button1_Click"
        Text="Add 2 years to the Publication Date!" /></p>
</form>
</body>
</html>
<%@ Page LANGUAGE="VB" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server" >

  Private Sub Button1_Click(sender As Object, e As EventArgs)

    Dim myXml As New XmlDocument
    myXml = CType(XmlSource.GetXmlDocument(), XmlDocument)

    Dim path As String = "bookstore/book/@publicationdate"
    Dim nodeList As XmlNodeList = myXml.SelectNodes(path)

    Dim aDate As XmlNode
    For Each aDate In  nodeList
      Dim helper As Integer = Int32.Parse(aDate.Value) + 2
      aDate.Value = helper.ToString()
    Next aDate

    XmlSource.Save()
    Repeater1.DataBind()

  End Sub 'Button1_Click
</script>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server" >

      <asp:XmlDataSource
        runat="server"
        ID="XmlSource"
        XPath="bookstore/book[@genre='novel']"
        DataFile="Booksort2.xml"
        EnableViewState="True"
        EnableCaching="False" />

      <asp:Repeater
        runat="server"
        ID="Repeater1"
        DataSourceID="XmlSource" >
          <ItemTemplate >
            <h1><%# XPath ("title/text()") %> </h1>
              <b>Author:</b><%# XPath ("author/first-name/text()") %> <%# XPath ("author/last-name/text()") %>
              <b>PublicationDate:</b><%# XPath ("@publicationdate") %>
              <b>Price:</b><%# XPath ("price/text()") %>
          </ItemTemplate>
      </asp:Repeater>


      <p><asp:Button
        runat="server"
        ID="Button1"
        onclick="Button1_Click"
        Text="Add 2 years to the Publication Date!" /></p>
</form>
</body>
</html>

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

<?xml version="1.0" encoding="utf-8"?>  
 <bookstore xmlns:bk="urn:samples">  
   <book genre="novel" publicationdate="1999" bk:ISBN="0000000000">  
     <title>Secrets of Silicon Valley</title>  
     <author>  
       <first-name>Sheryl</first-name>  
       <last-name>Hunter</last-name>  
     </author>  
     <price>24.95</price>"   
     </book>  
   <book genre="novel" publicationdate="1985" bk:ISBN="1111111111">  
     <title>Straight Talk About Computers</title>  
     <author>  
       <first-name>Dean</first-name>  
       <last-name>Straight</last-name>  
     </author>  
     <price>29.95</price>  
   </book>  
</bookstore>  

備註

XmlDataSource雖然控制項通常用於唯讀資料系結案例中,但您可以使用 XmlDataSource 控制項來編輯基礎 XML 資料檔案中的 XML 資料。 在這些案例中,XML 資料會由 XmlDataSource 控制項從 XML 檔案載入。 您可以使用 方法修改 XmlDataDocument 記憶體 GetXmlDocument 中的 ,然後藉由呼叫 Save 方法來儲存至 XML 資料檔案。 當符合下列條件時,就可以進行這個可編輯的 XML 案例:

  • XML 資料是從 屬性所指示 DataFile 的 XML 檔案載入,而不是從 屬性中指定的 Data 內嵌 XML 資料載入。

  • TransformFile 屬性中 Transform 未指定任何 XSLT 轉換。

方法 Save 不會處理不同要求的並行儲存作業。 如果多個使用者透過 XmlDataSource 控制項編輯 XML 檔案,則不保證所有使用者都使用相同的資料操作。 作業也可能 Save 因為這些相同的並行問題而失敗。

適用於

另請參閱