//</Snippet9> //<Snippet10> 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(); } } }
//</Snippet8> //<Snippet9> private void ModifyCachedString(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)) { //<Snippet11> //<Snippet12> serverDocument1 = new ServerDocument(documentPath); CachedDataHostItem hostItem1 = serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"]; CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"]; //</Snippet12> if (dataItem1 != null && Type.GetType(dataItem1.DataType) == typeof(string)) { dataItem1.SerializeDataInstance("This is the new cached string value."); serverDocument1.Save(); } //</Snippet11> } 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(); } } }
//</Snippet4> //<Snippet5> private void DisplayDataCacheContents(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); System.Text.StringBuilder stringBuilder1 = new System.Text.StringBuilder(); // Display all of the cached data items // in the document. foreach (CachedDataHostItem hostItem1 in serverDocument1.CachedData.HostItems) { stringBuilder1.Append("\nNamespace and class: "); stringBuilder1.Append(hostItem1.Id + "\n"); foreach (CachedDataItem dataItem1 in hostItem1.CachedData) { stringBuilder1.Append(" Data item: "); stringBuilder1.Append(dataItem1.Id + "\n"); } } MessageBox.Show(stringBuilder1.ToString()); } 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(); } } }