Share via


CachedDataItem.SerializeDataInstance (Método)

Serializa los datos en el objeto de datos almacenado en memoria caché representado por CachedDataItem.

Espacio de nombres:  Microsoft.VisualStudio.Tools.Applications
Ensamblado:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (en Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintaxis

'Declaración
Public Sub SerializeDataInstance ( _
    value As Object _
)
public void SerializeDataInstance(
    Object value
)

Parámetros

  • value
    Tipo: System.Object
    Objeto que contiene los datos que desea guardar en un objeto en la caché de datos.

Comentarios

Utilice el método SerializeDataInstance para inicializar o modificar el valor de un objeto de datos almacenado en memoria caché. Este método serializa el parámetro value en el objeto de datos almacenado en memoria caché que CachedDataItem representa. Para modificar valores de datos concretos en un objeto de datos complejo almacenado en memoria caché, como DataSet o DataTable, deserialice la representación XML de los datos almacenados en memoria caché en una nueva instancia del objeto en caché, realice los cambios en esta copia y, a continuación, utilice el método SerializeDataInstance para volver a serializar los cambios en la caché de datos. Para obtener más información, vea Acceso a datos de documentos en el servidor y Cómo: Cambiar los datos almacenados en caché en un libro ubicado en un servidor.

Este método utiliza el formato DiffGram para serializar objetos de DataSet, de DataTable y de conjuntos de datos con tipo en la caché de datos. De este modo, se garantiza que los cambios en la caché de datos de un documento sin conexión se envían correctamente al servidor. Para obtener más información, vea DiffGrams (ADO.NET).

Ejemplos

En el ejemplo de código siguiente se utiliza el método SerializeDataInstance para modificar el valor de una cadena almacenada en memoria caché en una hoja de cálculo de un libro de Excel.

Este ejemplo necesita:

  • Una personalización de nivel de documento para Excel que tiene una clase Sheet1 en el espacio de nombres ExcelWorkbook1 y una cadena almacenada en memoria caché en la clase Sheet1 denominada CachedString.

  • Un proyecto de aplicación de consola o algún otro proyecto que no es de Office.

  • Referencias a los siguientes ensamblados:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll y Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si el destino del proyecto es .NET Framework 4).

      O bien

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si el proyecto está dirigido a .NET Framework 3.5).

  • Instrucciones Imports (para Visual Basic) o using (para C#) para los espacios de nombres Microsoft.VisualStudio.Tools.Applications y Microsoft.VisualStudio.Tools.Applications.Runtime en la parte superior de su archivo 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();
    }
}

Seguridad de .NET Framework

Vea también

Referencia

CachedDataItem Clase

Microsoft.VisualStudio.Tools.Applications (Espacio de nombres)

Otros recursos

Acceso a datos de documentos en el servidor