XmlDataSource.Save Metodo

Definizione

Salva i dati XML al momento in memoria utilizzando il controllo XmlDataSource su disco se è impostata la proprietà DataFile.

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

Eccezioni

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

-oppure-

Un URL viene specificato per la proprietà DataFile. Il controllo XmlDataSource tuttavia non dispone delle autorizzazioni corrette per la risorsa Web.

Un URL è specificato per la proprietà DataFile; tuttavia, non è un URL basato su HTTP.

-oppure-

Nella finestra di progettazione non è stato eseguito in modo corretto il mapping di un percorso relativo alla fase di progettazione prima di utilizzare il controllo XmlDataSource.

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

Esempio

In questa sezione sono riportati due esempi di codice. Nel primo esempio di codice viene illustrato come utilizzare un XmlDataSource controllo con un TreeView controllo per visualizzare e modificare i dati XML contenuti in un file XML. Nel secondo esempio di codice 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.

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

Anche se il XmlDataSource controllo viene in genere usato in scenari di data binding di sola lettura, è possibile usare 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 . È possibile 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à .

  • Nessuna trasformazione XSLT specificata nelle Transform proprietà o TransformFile .

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 vi è alcuna garanzia 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