Método CachedDataItem.SerializeDataInstance

Serializa os dados para o objeto de dados em cache é representado pela CachedDataItem.

Namespace:  Microsoft.VisualStudio.Tools.Applications
Assembly:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (em Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintaxe

'Declaração
Public Sub SerializeDataInstance ( _
    value As Object _
)
public void SerializeDataInstance(
    Object value
)

Parâmetros

  • value
    Tipo: System.Object
    O objeto que contém os dados que você deseja salvar em um objeto no cache de dados.

Comentários

Use o SerializeDataInstance método para inicializar ou modificar o valor de um objeto de dados em cache. Esse método serializa o value parâmetro nos dados armazenados em cache de objeto que o CachedDataItem representa. Para fazer alterações nos valores de dados específicos em um objeto em cache de dados complexos, como um DataSet ou DataTable, desserializar a representação XML dos dados armazenados em cache em uma nova instância do objeto em cache, faça as alterações dessa cópia e, em seguida, use o SerializeDataInstance método para serializar as alterações de volta para o cache de dados. For more information, see Acesso a dados em documentos no servidor and Como: Alterar dados em uma pasta de trabalho em um servidor de cache.

Este método usa o formato de DiffGram para serializar DataSet, DataTablee digitou objetos dataset em cache de dados. Isso garante que as alterações para o cache de dados em um documento off-line são enviadas ao servidor corretamente. For more information, see DiffGrams (ADO.NET).

Exemplos

O seguinte exemplo de código usa a SerializeDataInstance método para modificar o valor de uma seqüência que é armazenado em cache em uma planilha de uma pasta de trabalho do Excel.

This example requires:

  • Uma personalização em nível de documento para o Excel tem um Sheet1 classe na ExcelWorkbook1 namespace e uma seqüência de caracteres armazenados em cache na Sheet1 classe nomeada CachedString.

  • Um projeto de aplicativo de console ou algum outro projeto de não-Office.

  • Referências aos assemblies seguintes:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.dll (se os destinos do projeto de .NET Framework 4).

      or

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (se os destinos do projeto a.NET Framework 3.5).

  • Imports(para Visual Basic) ou using (para C#) instruções para Microsoft.VisualStudio.Tools.Applications e Microsoft.VisualStudio.Tools.Applications.Runtime namespaces no topo do seu arquivo de código.

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();
    }
}

Segurança do .NET Framework

Consulte também

Referência

CachedDataItem Classe

Namespace Microsoft.VisualStudio.Tools.Applications

Outros recursos

Acesso a dados em documentos no servidor