Metodo CachedDataItem.SerializeDataInstance

Serializza i dati nell'oggetto dati memorizzato nella cache che è rappresentato da CachedDataItem.

Spazio dei nomi:  Microsoft.VisualStudio.Tools.Applications
Assembly:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (in Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintassi

'Dichiarazione
Public Sub SerializeDataInstance ( _
    value As Object _
)
public void SerializeDataInstance(
    Object value
)

Parametri

  • value
    Tipo: System.Object
    Oggetto contenente i dati da salvare in oggetto nella cache di dati.

Note

Utilizzare il metodo SerializeDataInstance per inizializzare o modificare il valore di un oggetto dati memorizzato nella cache. Questo metodo serializza il parametro value nell'oggetto dati memorizzato nella cache che CachedDataItem rappresenta. Per apportare modifiche ai valori di dati specifici in un oggetto dati complesso memorizzato nella cache, come un oggetto DataSet o DataTable, deserializzare la rappresentazione XML dei dati memorizzati nella cache in una nuova istanza dell'oggetto memorizzato nella cache, apportare le modifiche a tale copia, quindi utilizzare il metodo SerializeDataInstance per serializzare nuovamente le modifiche nella cache di dati. Per ulteriori informazioni, vedere Accesso ai dati dei documenti sul server e Procedura: modificare i dati memorizzati nella cache di una cartella di lavoro di un server.

Questo metodo utilizza il formato DiffGram per serializzare gli oggetti DataSet, DataTable e gli oggetti DataSet tipizzati nella cache di dati. In questo modo le modifiche alla cache di dati in un documento offline vengono inviate al server correttamente. Per ulteriori informazioni, vedere DiffGram (ADO.NET).

Esempi

Nell'esempio di codice seguente viene utilizzato il metodo SerializeDataInstance per modificare il valore di una stringa memorizzata nella cache in un foglio di lavoro di una cartella di lavoro di Excel.

Per questo esempio sono necessari i seguenti requisiti:

  • Personalizzazione a livello di documento per Excel che dispone di una classe Sheet1 nello spazio dei nomi ExcelWorkbook1 e di una stringa memorizzata nella cache nella classe Sheet1 denominata CachedString.

  • Progetto di applicazione console o qualche altro progetto non-Office.

  • Riferimenti ai seguenti assembly:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.dll (se il progetto è destinato a .NET Framework 4).

      oppure

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (se il progetto è destinato a .NET Framework 3.5).

  • Istruzioni Imports (per Visual Basic) o using (per C#) per gli spazi dei nomi Microsoft.VisualStudio.Tools.Applications e Microsoft.VisualStudio.Tools.Applications.Runtime all'inizio del file di codice

Private Sub ModifyCachedString(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion <> 3 Then
            MessageBox.Show("This document does not have a Visual Studio Tools for Office " & _
                "customization, or it has a customization that was created with a version of " & _
                "the runtime that is incompatible with this version of the ServerDocument class.")
            Return
        End If

        If ServerDocument.IsCacheEnabled(documentPath) Then
            serverDocument1 = New ServerDocument(documentPath)
            Dim hostItem1 As CachedDataHostItem = _
                serverDocument1.CachedData.HostItems("ExcelWorkbook1.Sheet1")
            Dim dataItem1 As CachedDataItem = hostItem1.CachedData("CachedString")

            If dataItem1 IsNot Nothing AndAlso _
                Type.GetType(dataItem1.DataType).Equals(GetType(String)) Then

                dataItem1.SerializeDataInstance("This is the new cached string value.")
                serverDocument1.Save()
            End If
        Else
            MessageBox.Show("The specified document does not have cached data.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
    End Try
End Sub
private void ModifyCachedString(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
        {
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");
            return;
        }

        if (ServerDocument.IsCacheEnabled(documentPath))
        {
            serverDocument1 = new ServerDocument(documentPath);
            CachedDataHostItem hostItem1 = 
                serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
            CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

            if (dataItem1 != null &&
                Type.GetType(dataItem1.DataType) == typeof(string))
            {
                dataItem1.SerializeDataInstance("This is the new cached string value.");
                serverDocument1.Save();
            }
        }
        else
        {
            MessageBox.Show("The specified document does not have cached data.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
    }
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

CachedDataItem Classe

Spazio dei nomi Microsoft.VisualStudio.Tools.Applications

Altre risorse

Accesso ai dati dei documenti sul server