Sdílet prostřednictvím


CachedDataItem.Xml – vlastnost

Získá nebo nastaví reprezentaci XML data uložená v mezipaměti objektu, který je zastoupen CachedDataItem.

Obor názvů:  Microsoft.VisualStudio.Tools.Applications
Sestavení:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (v Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Syntaxe

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

Hodnota vlastnosti

Typ: System.String
Data uložená v mezipaměti objektu, který je zastoupen ve formátu XML CachedDataItem.

Poznámky

Chcete-li získat hodnotu data uložená v mezipaměti objektu, použijte Xml vlastnost rekonstruovat reprezentaci XML dat uložených v mezipaměti do nové instance objektu data v mezipaměti.Můžete provést změny v této kopii a serializovat změny zpět do mezipaměti data.

Ve většině případů můžete použít SerializeDataInstance metoda serializace změněný objekt do mezipaměti data.Pokud chcete provést vlastní serializaci změny data v mezipaměti, můžete také napsat přímo Xml vlastnost.Však pokud provádíte změny DataSet, DataTable, nebo zadaný objekt dataset, který bude aktualizován k databázi pomocí DataAdapter, zadejte formát DiffGram formát zápisu změny do mezipaměti.Jinak se změny DataSet nebo DataTable bude přidán do databáze nové řádky namísto změněné řádky.Další informace naleznete v tématu Přístup k datům v dokumentech na serveru.

Příklady

Následující kód například používá Xml vlastnost získat hodnotu řetězce, který je uložen do mezipaměti listu sešitu aplikace Excel.Příklad zobrazí hodnotu v poli zprávy.

Tento příklad vyžaduje:

  • Úroveň dokumentu přizpůsobení aplikace Excel, který má Sheet1 třídy v ExcelWorkbook1 oboru názvů a řetězec v mezipaměti Sheet1 třídu s názvem CachedString.

  • Projekt aplikace konzoly nebo některé jiné-Office project.

  • Odkazy na následující sestavení:

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

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

  • Imports(pro Visual Basic) nebo using (pro C#) výkazy pro Microsoft.VisualStudio.Tools.Applications a Microsoft.VisualStudio.Tools.Applications.Runtime obory názvů v horní části kódu souboru.

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

Například kód, který demonstruje použití Xml vlastnost serializovat změny mezipaměti a DataSet, viz Návod: Změna dat uložených v mezipaměti sešitu na serveru.

Zabezpečení rozhraní .NET Framework

Viz také

Referenční dokumentace

CachedDataItem Třída

Microsoft.VisualStudio.Tools.Applications – obor názvů