CachedDataItem.Xml Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the XML representation of the cached data object that is represented by the CachedDataItem.
public:
property System::String ^ Xml { System::String ^ get(); void set(System::String ^ value); };
public string Xml { get; set; }
member this.Xml : string with get, set
Public Property Xml As String
Property Value
The XML representation of the cached data object that is represented by the CachedDataItem.
Examples
The following code example uses the Xml property to get the value of a string that is cached in a worksheet of an Excel workbook. The example displays the value in a message box.
This example requires:
A document-level customization for Excel that has a
Sheet1
class in theExcelWorkbook1
namespace, and a cached string in theSheet1
class namedCachedString
.A console application project or some other non-Office project.
References to the following assemblies:
Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
Microsoft.VisualStudio.Tools.Applications.Runtime.dll
Imports
(for Visual Basic) orusing
(for C#) statements for Microsoft.VisualStudio.Tools.Applications and Microsoft.VisualStudio.Tools.Applications.Runtime namespaces at the top of your code file.
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();
}
}
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
For a code example that demonstrates how to use the Xml property to modify and serialize changes to a cached DataSet, see Walkthrough: Changing Cached Data in a Workbook on a Server.
Remarks
To get the value of a cached data object, use the Xml property to deserialize the XML representation of the cached data into a new instance of the cached data object. You can then make changes to this copy and serialize the changes back into the data cache.
In most cases, you can use the SerializeDataInstance method to serialize the changed object into the data cache. If you want to perform your own serialization for changes to cached data, you can also write directly to the Xml property. However, if you are making changes to a DataSet, DataTable, or a typed dataset that will be updated to a database by using a DataAdapter, specify the DiffGram format when you write the changes to the cached data. Otherwise, the changes to the DataSet or DataTable will be added to the database as new rows instead of changed rows. For more information, see Accessing Data in Documents on the Server.