Reading and Writing the Document Data Cache

In the preceding section you created the test document that you will use to read and write the data using the ServerDocument API. In the following steps, you will create a new Console application that will read and write the cached data fields from the document.

1. Create a new Visual Basic Console application project.

2. Add a reference to the VSTO runtime, as shown in Figure 13.4. On the Project menu, click Add Reference. In the .NET tab of the Add References dialog box, select Microsoft.VisualStudio.Tools.Applica-tions.Runtime.

■ NET I COM ] Projects ] Browse ] Recent ]

Component Name

1 Version

1 Runtime

-1

Microsoft. VisualStudio .Template Wizardinterface

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .TextManager. Interop

7.1.1030...

VL 1.4322

Microsoft. VisualStudio .TextManager. Interop .8.0

8.0.0.0

vt 1.4322

Microsoft. VisualStudio .TextTemplating

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .TextTemplating. VSHost

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .Tools. Applications. Adapter

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .Tools. Applications. AddlnManager

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .Tools. Applications. Blueprints

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .Tools. Applications. ComRPCChannel

8.0.0.0

»2.0.50727

1

Microsoft. VisualStudio .Tools. Applications. Contract

8.0.0.0

»2.0.50727

_1

Microsoft. VisualStudio .Tools. Applications. DesignTime

8.0.0.0

»2.0.50727

Microsoft. VisualStudio .Tools. Applications. Interop Adapter

8.0.0.0

»2.0.50727

Microsoft. VisualStudio. VCCodeModel

8.0.0.0

ïl.0.3705

Microsoft. VisualStudio. VCProject

8.0.0.0

vL0.3705

zi

<i 1

OK I Cancel

Figure 13.4. Adding a reference to the VSTO runtime

3. Add an Imports statement for the runtime to the Module1.vb class file. Use the code in Listing 13.6.

Listing 13.6. Adding an Imports statement for the VSTO runtime Imports Microsoft.VisualStudio.Tools.Applications.Runtime

4. Create a ServerDocument object by passing the path to the document in the constructor, as shown in Listing 13.7. In this example you use a Console application, but this could also be called on the server in an ASP.NET application, in a Windows Forms application on the client, or even in the VSTO application itself.

Listing 13.7. Creating an instance of the ServerDocument class

Dim documentPath As String = _

"C:\CachedDataSample\CachedDataSample\bin\Debug\" & _ "ExcelWorkbook1.xls"

'Create the ServerDocument class.

Dim myServerDocument As New ServerDocument(documentPath)

5. Add the code in Listing 13.8 to create a CachedData object from the ServerDocument object.

Listing 13.8. Getting an instance of the CachedData class

' Get the cached data from the document.

Dim myCachedData As CachedData = myServerDocument.CachedData

6. Add the code in Listing 13.9 to create a CachedDataHostItem object from the CachedData object.

Listing 13.9. Getting an instance of the CachedDataHostItem class

' Get the cached data for Sheet1.

Dim sheet1CachedData As CachedDataHostItem = _

myCachedData.HostItems("ExcelWorkbook1.Sheet1")

7. Add the code in Listing 13.10 to get the CachedDataItem objects from the CachedDataHostItem object. The CachedDataItem object represents the individual cached items in the data cache. Each item that you mark as cached in the document solution will have one CachedDataItem.

Listing 13.10. Getting the instances of the CachedDataltem class

Dim FirstNameCachedData As CachedDataItem = _

sheet1CachedData.CachedData("CachedFirstName") Dim LastNameCachedData As CachedDataItem = _

sheet1CachedData.CachedData("CachedLastName")

Next, you need to convert the CachedDataItem Xml objects to String objects to get the cached values, a process known as deserialization. The CachedDataItem class has a property called Xml that contains the XML serialized data. It also has a DataType property, which is the type of the data that is serialized in the Xml property. There is also a property called Schema, which contains the schema for the Xml property, but only if the type is a DataTable or a DataSet.

The CachedDataItem object does not provide any methods to deserialize the data that is stored in the Xml property. You need to implement this functionality because this API is designed to be run on the server.

When you use XmlSerializer to deserialize the data, you might not get optimum performance. You also need to consider issues with version-ing. For example, if you serialize the data on the client using version 1 of your solution and then try to deserialize it using version 2, problems may arise. For best performance, you should use System.Xml's document object model (DOM) to parse the XML manually and extract the values. This approach works for any type and version and doesn't require that the client types and assemblies be installed on the server.

In this example, you use XmlSerializer because these are simple string types. To deserialize the FirstName and LastName values, add the code in Listing 13.11 to the top of the module, followed by the code in Listing 13.12. In this example you don't use the deserialized values after you create them.

Listing 13.11. Namespaces needed to deserialize the data cache

' Namespaces for deserialization. Imports System.Xml.Serialization Imports System.IO

Listing 13.12. Deserializing the instances of the CachedDataltem class ' Get the FirstName value.

Dim FirstNameSerializer As New XmlSerializer( _ Type.GetType(FirstNameCachedData.DataType)) Dim FirstNameXml As New Xml.XmlTextReader( _

New StringReader(FirstNameCachedData.Xml)) Dim FirstName As String = _

FirstNameSerializer.Deserialize(FirstNameXml)

' Get the LastName value.

Dim LastNameSerializer As New XmlSerializer( _ Type.GetType(LastNameCachedData.DataType)) Dim LastNameXml As New Xml.XmlTextReader( _

New StringReader(LastNameCachedData.Xml)) Dim LastName As String = LastNameSerializer.Deserialize( _ LastNameXml)

Now you can set the FirstName and LastName CachedDataltems. The CachedDataltem class provides a method to automatically serialize the data into the Xml property. You call the SerializeDatalnstance method, passing as the parameter the object to store. In this example, you are passing a string to update the cached data values. Add the code in Listing 13.13 to set the cached data values.

Listing 13.13. Setting the instances of the CachedDataltem class

FirstNameCachedData.SerializeDataInstance("Kathleen") LastNameCachedData.SerializeDataInstance("McGrath")

The final step is to save and close the document to persist the changes. Add the code in Listing 13.14 to save and close the document.

Listing 13.14. Saving and closing the document myServerDocument.Save() myServerDocument.Close()

0 0

Post a comment