Edit

Share via


XmlDataSource.Save Method

Definition

Saves the XML data currently held in memory by the XmlDataSource control to disk if the DataFile property is set.

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

Exceptions

XML data was loaded using the Data property instead of the DataFile property.

-or-

A URL is specified for the DataFile property; however, the XmlDataSource control does not have the correct permissions for the Web resource.

A URL is specified for the DataFile property; however, it is not an HTTP-based URL.

-or-

A design-time relative path was not mapped correctly by the designer before using the XmlDataSource control.

Access is denied to the path specified for the DataFile property.

Examples

This section contains two code examples. The first code example demonstrates how to use an XmlDataSource control with a TreeView control to display and edit XML data contained in an XML file. The second code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display and edit XML data contained in an XML file.

The following code example demonstrates how to use an XmlDataSource control with a TreeView control to display and edit XML data contained in an XML file. The data is manipulated in memory using the GetXmlDocument method every time you select a TreeView node, and is then saved to the XML file. Finally, DataBind is called on the TreeView control to refresh the data that it displays.

<%@ 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>

The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display and edit XML data contained in an XML file. As with the previous example, the data is manipulated in memory using the XmlDataDocument object retrieved by the GetXmlDocument method. Finally, DataBind is called on the TreeView control to refresh the data that it displays.

<%@ 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>

The XML file in the code examples has the following data:

<?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>  

Remarks

While the XmlDataSource control is typically used in read-only data-binding scenarios, you can use the XmlDataSource control to edit XML data in the underlying XML data file. In these scenarios, XML data is loaded from an XML file by the XmlDataSource control. You modify the XmlDataDocument in memory using the GetXmlDocument method, and then save to the XML data file by calling the Save method. This editable XML scenario is possible when the following conditions are met:

  • The XML data is loaded from an XML file indicated by the DataFile property, not from inline XML data specified in the Data property.

  • No XSLT transformation is specified in the Transform or TransformFile properties.

The Save method does not handle concurrent save operations by different requests. If more than one user is editing an XML file through the XmlDataSource control, there is no guarantee that all users are operating with the same data. It is also possible for a Save operation to fail due to these same concurrency issues.

Applies to

See also