Właściwość CachedDataItem.Xml —
Zwraca lub ustawia reprezentacji XML obiektu buforowanych danych, która jest reprezentowana przez CachedDataItem.
Przestrzeń nazw: Microsoft.VisualStudio.Tools.Applications
Zestaw: Microsoft.VisualStudio.Tools.Applications.ServerDocument (w Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)
Składnia
'Deklaracja
Public Property Xml As String
public string Xml { get; set; }
Wartość właściwości
Typ: System.String
Reprezentacji XML obiektu buforowanych danych, która jest reprezentowana przez CachedDataItem.
Uwagi
Aby uzyskać wartość obiektów buforowanych danych, należy użyć Xml właściwość, aby deserializować reprezentacji XML buforowanych danych do nowego wystąpienia obiektów buforowanych danych.Następnie można wprowadzić zmiany do tej kopii i serializować zmian z powrotem w pamięci podręcznej danych.
W większości przypadków można użyć SerializeDataInstance metoda serializować obiektów zmienionych w pamięci podręcznej danych.Jeśli chcesz wykonać swoje własne serializacji dla zmiany danych w pamięci podręcznej, można także pisać bezpośrednio do Xml właściwości.Jednakże jeśli są dokonywanie zmian w DataSet, DataTable, lub dataset maszynowy, który zostanie zaktualizowany do bazy danych przy użyciu DataAdapter, określ format w formacie DiffGram, podczas zapisywania zmian buforowane dane.W przeciwnym razie zmiany do DataSet lub DataTable zostaną dodane do bazy danych jako nowe wiersze zamiast zmienionych wierszy.Aby uzyskać więcej informacji, zobacz Uzyskiwanie dostępu do danych w dokumentach na serwerze.
Przykłady
Następujący kod w przykładzie wykorzystano Xml właściwość, aby uzyskać wartość ciągu, który jest buforowany w arkuszu skoroszytu programu Excel.Przykład wyświetla wartość w polu komunikat.
W tym przykładzie wymaga:
Dostosowywanie poziomie dokumentu do programu Excel, który ma Sheet1 klasy w ExcelWorkbook1 obszaru nazw i ciąg znaków buforowanych w Sheet1 klasę o nazwie CachedString.
Projekt aplikacji konsoli lub innego projektu nienależących do pakietu Office.
Odwołania do następujących zestawów:
Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
Microsoft.VisualStudio.Tools.Applications.Runtime.dll
Imports(dla języka Visual Basic) lub using (w języku C#) oświadczenia dla Microsoft.VisualStudio.Tools.Applications i Microsoft.VisualStudio.Tools.Applications.Runtime nazw u góry pliku kodu.
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();
}
}
Na przykład kod, który demonstruje, jak używać Xml właściwość, aby zmodyfikować i serializować zmiany do pamięci podręcznej DataSet, zobacz Wskazówki: zmiana danych z pamięci podręcznej ze skoroszytu na serwerze.
Zabezpieczenia programu .NET Framework
- Pełne zaufanie do bezpośredniego wywołującego. Tego elementu członkowskiego nie można używać w kodzie częściowo zaufanym. Aby uzyskać więcej informacji, zobacz Używanie bibliotek pochodzących z częściowo zaufanego kodu.