Condividi tramite


XmlDataSource.Save Metodo

Definizione

Salva i dati XML attualmente contenuti in memoria dal XmlDataSource controllo su disco se la DataFile proprietà è impostata.

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

Eccezioni

I dati XML sono stati caricati utilizzando la Data proprietà anziché la DataFile proprietà .

oppure

Per la DataFile proprietà viene specificato un URL. Tuttavia, il XmlDataSource controllo non dispone delle autorizzazioni corrette per la risorsa Web.

Viene specificato un URL per la DataFile proprietà, ma non è un URL basato su HTTP.

oppure

Un percorso relativo in fase di progettazione non è stato mappato correttamente dalla finestra di progettazione prima di usare il XmlDataSource controllo .

L'accesso viene negato al percorso specificato per la DataFile proprietà .

Esempio

Questa sezione contiene due esempi di codice. Il primo esempio di codice illustra come utilizzare un XmlDataSource controllo con un TreeView controllo per visualizzare e modificare i dati XML contenuti in un file XML. Il secondo esempio di codice illustra come utilizzare un XmlDataSource controllo con un controllo basato su Repeater modelli per visualizzare e modificare i dati XML contenuti in un file XML.

Nell'esempio di codice seguente viene illustrato come utilizzare un XmlDataSource controllo con un TreeView controllo per visualizzare e modificare i dati XML contenuti in un file XML. I dati vengono modificati in memoria usando il GetXmlDocument metodo ogni volta che si seleziona un TreeView nodo e quindi vengono salvati nel file XML. DataBind Viene infine chiamato sul TreeView controllo per aggiornare i dati visualizzati.

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

Nell'esempio di codice seguente viene illustrato come utilizzare un XmlDataSource controllo con un controllo basato su Repeater modelli per visualizzare e modificare i dati XML contenuti in un file XML. Come nell'esempio precedente, i dati vengono modificati in memoria usando l'oggetto XmlDataDocument recuperato dal GetXmlDocument metodo . DataBind Viene infine chiamato sul TreeView controllo per aggiornare i dati visualizzati.

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

Il file XML negli esempi di codice contiene i dati seguenti:

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

Commenti

Mentre il XmlDataSource controllo viene in genere usato in scenari di data binding di sola lettura, è possibile utilizzare il XmlDataSource controllo per modificare i dati XML nel file di dati XML sottostante. In questi scenari, i dati XML vengono caricati da un file XML dal XmlDataSource controllo . Modificare in XmlDataDocument memoria utilizzando il GetXmlDocument metodo e quindi salvare nel file di dati XML chiamando il Save metodo . Questo scenario XML modificabile è possibile quando vengono soddisfatte le condizioni seguenti:

  • I dati XML vengono caricati da un file XML indicato dalla DataFile proprietà , non dai dati XML inline specificati nella Data proprietà .

  • Nelle proprietà o TransformFile non è specificata Transform alcuna trasformazione XSLT.

Il Save metodo non gestisce le operazioni di salvataggio simultanee da richieste diverse. Se più utenti modificano un file XML tramite il XmlDataSource controllo , non è garantito che tutti gli utenti funzionino con gli stessi dati. È anche possibile che un'operazione Save non riesca a causa di questi stessi problemi di concorrenza.

Si applica a

Vedi anche