CachedDataItem.Xml 属性 (2007 system)

更新:2007 年 11 月

获取或设置 CachedDataItem 表示的缓存数据对象的 XML 表示形式。

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

语法

声明
Public Property Xml As String
用法
Dim instance As CachedDataItem
Dim value As String

value = instance.Xml

instance.Xml = value
public string Xml { get; set; }

属性值

类型:System.String

CachedDataItem 表示的缓存数据对象的 XML 表示形式。

备注

若要获取缓存数据对象的值,请使用 Xml 属性将缓存数据的 XML 表示形式反序列化到缓存数据对象的一个新实例中。然后,可以对此副本进行更改并将更改重新序列化到数据缓存中。

在大多数情况下,您可以使用 SerializeDataInstance 方法将更改过的对象序列化为数据缓存。如果想要对缓存数据更改执行自己的序列化,还可以直接写入 Xml 属性。但是,如果要使用 DataAdapter 对将更新到数据库的 DataSetDataTable 或类型化数据集进行更改,请在将更改写入到缓存数据时指定 DiffGram 格式。否则,对 DataSetDataTable 的更改将作为新行而不是经过更改的行添加到数据库中。有关更多信息,请参见访问服务器上的文档数据

示例

下面的代码示例使用 Xml 属性获取在 Excel 工作簿的工作表中缓存的字符串的值。此示例将该值显示在一个消息框中。

此示例需要在代码文件顶部使用对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll 程序集的引用,并对 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications 命名空间使用 Imports(对于 Visual Basic)或 using(对于 C#)语句。此示例还假定指定工作簿具有一个自定义项,该自定义项在 Sheet1 命名空间中有一个 Sheet1 类,在 Sheet1 类中有一个名为 CachedString 的缓存字符串。

Private Sub ReadCachedStringValue(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

                Using stringReader As New System.IO.StringReader(dataItem1.Xml)
                    Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(String))
                    Dim cachedString As String = serializer.Deserialize(stringReader)
                    MessageBox.Show("The value of CachedString is: " + cachedString)
                End Using
            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 ReadCachedStringValue(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))
            {
                using (System.IO.StringReader stringReader =
                    new System.IO.StringReader(dataItem1.Xml))
                {
                    System.Xml.Serialization.XmlSerializer serializer =
                        new System.Xml.Serialization.XmlSerializer(typeof(string));
                    string cachedString = serializer.Deserialize(stringReader) as string;
                    MessageBox.Show("The value of CachedString is: " + cachedString);
                }
            }
        }
        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();
    }
}

有关演示如何使用 Xml 属性进行修改并将更改序列化到缓存的 DataSet 的代码示例,请参见如何:更改服务器上的工作簿中的缓存数据

权限

另请参见

参考

CachedDataItem 类

CachedDataItem 成员

Microsoft.VisualStudio.Tools.Applications 命名空间