Proprietà CachedDataItem.Xml

Ottiene o imposta la rappresentazione XML dell'oggetto dati memorizzato rappresentato dalla proprietà CachedDataItem.

Spazio dei nomi:  Microsoft.VisualStudio.Tools.Applications
Assembly:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (in Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintassi

'Dichiarazione
Public Property Xml As String
public string Xml { get; set; }

Valore proprietà

Tipo: System.String
Rappresentazione XML dell'oggetto dati memorizzato rappresentato dalla proprietà CachedDataItem.

Note

Per ottenere il valore di un oggetto dati memorizzato nella cache, utilizzare la proprietà Xml per deserializzare la rappresentazione XML dei dati memorizzati nella cache in una nuova istanza dell'oggetto dati memorizzato nella cache.È quindi possibile apportare modifiche a questa copia e serializzare di nuovo le modifiche nella cache di dati.

Nella maggioranza dei casi, è possibile utilizzare il metodo SerializeDataInstance per serializzare l'oggetto modificato nella cache di dati.Se si desidera eseguire la serializzazione personalizzata per le modifiche ai dati memorizzati nella cache, è anche possibile scrivere direttamente nella proprietà Xml.Se tuttavia si apportano modifiche a un oggetto DataSet, DataTable oppure a un dataset tipizzato che verrà aggiornato in base a un database mediante un oggetto DataAdapter, specificare il formato DiffGram durante scrittura delle modifiche nei dati memorizzati nella cache.In caso contrario, le modifiche apportate all'oggetto DataSet o DataTable verranno aggiunte al database come nuove righe anziché come righe modificate.Per ulteriori informazioni, vedere Accesso ai dati dei documenti sul server.

Esempi

Nell'esempio di codice seguente viene utilizzata la proprietà Xml per ottenere il valore di una stringa memorizzata nella cache in un foglio di lavoro di una cartella di lavoro di Excel.Nell'esempio il valore viene visualizzato in una finestra di messaggio.

L'esempio presenta i seguenti requisiti:

  • Personalizzazione a livello di documento per Excel che dispone di una classe Sheet1 nello spazio dei nomi ExcelWorkbook1 e di una stringa memorizzata nella cache nella classe Sheet1 denominata CachedString.

  • Progetto di applicazione console o qualche altro progetto non-Office.

  • Riferimenti ai seguenti assembly:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll

    • Microsoft.VisualStudio.Tools.Applications.Runtime.dll

  • Istruzioni Imports (per Visual Basic) o using (per C#) per gli spazi dei nomi Microsoft.VisualStudio.Tools.Applications e Microsoft.VisualStudio.Tools.Applications.Runtime all'inizio del file di codice

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

Per un esempio di codice che illustri l'utilizzo della proprietàXml per modificare e serializzare le modifiche a un oggetto DataSet memorizzato nella cache, vedere Procedura dettagliata: modifica dei dati memorizzati nella cache di una cartella di lavoro contenuta in un server.

Sicurezza di .NET Framework

Vedere anche

Riferimenti

CachedDataItem Classe

Spazio dei nomi Microsoft.VisualStudio.Tools.Applications