CachedDataItem.SerializeDataInstance 方法

将数据序列化到 CachedDataItem 所表示的缓存数据对象中。

命名空间:  Microsoft.VisualStudio.Tools.Applications
程序集:  Microsoft.VisualStudio.Tools.Applications.ServerDocument(在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)

语法

声明
Public Sub SerializeDataInstance ( _
    value As Object _
)
public void SerializeDataInstance(
    Object value
)

参数

  • value
    类型:System.Object
    一个对象,包含要保存到数据缓存中的对象的数据。

备注

使用 SerializeDataInstance 方法初始化或修改缓存数据对象的值。 此方法将 value 参数序列化到 CachedDataItem 所表示的缓存数据对象中。 若要更改复杂缓存数据对象(如 DataSetDataTable)中的特定数据值,请将缓存数据的 XML 表示形式反序列化到缓存对象的一个新实例中,对此副本进行更改,然后使用 SerializeDataInstance 方法将更改重新序列化到数据缓存中。 有关更多信息,请参见访问服务器上的文档数据如何:更改服务器上的工作簿中的缓存数据

此方法使用 DiffGram 格式将 DataSetDataTable 和类型化数据集对象序列化到数据缓存中。 此格式确保将对脱机文档中的数据缓存的更改正确发送到服务器。 有关更多信息,请参见 DiffGram (ADO.NET)

示例

下面的代码示例使用 SerializeDataInstance 方法修改在 Excel 工作簿的工作表中缓存的字符串的值。

此示例需要:

  • 在 ExcelWorkbook1 命名空间中有 Sheet1 类的 Excel 文档级自定义项,以及 Sheet1 类中名为 CachedString的缓存字符串。

  • 控制台应用程序项目或某些其他非 Office 项目。

  • 对下列程序集的引用:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll(如果项目以 .NET Framework 4 为目标)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll(如果该项目面向 .NET Framework 3.5)。

  • 位于代码文件顶部的用于 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 命名空间的Imports(对于 Visual Basic)或 using(对于 C#)语句。

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

.NET Framework 安全性

请参见

参考

CachedDataItem 类

Microsoft.VisualStudio.Tools.Applications 命名空间

其他资源

访问服务器上的文档数据