Share via


CachedDataItem.Xml (Propiedad)

Obtiene o establece la representación XML del 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 Property Xml As String
    Get
    Set
public string Xml { get; set; }

Valor de propiedad

Tipo: System.String
Representación XML del objeto de datos almacenado en memoria caché representado por CachedDataItem.

Comentarios

Para obtener el valor de un objeto de datos almacenado en memoria caché, utilice la propiedad Xml para deserializar la representación XML de los datos almacenados en memoria caché en una nueva instancia del objeto de datos almacenado en memoria caché. A continuación, puede modificar esta copia y volver a serializar los cambios en la caché de datos.

En la mayoría de los casos, puede utilizar el método SerializeDataInstance para serializar el objeto modificado en la caché de datos. Si desea realizar su propia serialización para llevar a cabo cambios en los datos almacenados en memoria caché, también puede escribir directamente en la propiedad Xml. Sin embargo, si va modificar un objeto DataSet o DataTable, o un conjunto de datos con tipo que se actualizará en una base de datos mediante una instancia de DataAdapter, especifique el formato DiffGram cuando escriba los cambios en los datos almacenados en memoria caché. De lo contrario, los cambios en el DataSet o DataTable se agregarán a la base de datos como nuevas filas en lugar de filas cambiadas. Para obtener más información, vea Acceso a datos de documentos en el servidor.

Ejemplos

En el ejemplo de código siguiente se utiliza la propiedad Xml para obtener el valor de una cadena almacenada en memoria caché en una hoja de cálculo de un libro de Excel. En el ejemplo se muestra el valor en un cuadro de mensaje.

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

Si desea obtener un ejemplo de código que muestre cómo utilizar la propiedad Xml para modificar y serializar cambios en un DataSet almacenados en memoria caché, vea Cómo: Cambiar los datos almacenados en caché en un libro ubicado en un servidor.

Seguridad de .NET Framework

Vea también

Referencia

CachedDataItem Clase

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