My Project
C:/Users/Lab_411_02/Desktop/Violet1 - dox/violet/violet/ControlStorage.cs
查看本檔案說明文件.
1 // -----------------------------------------------------------------------
2 // Copyright (c) 2011, The Outercurve Foundation.
3 // This software is released under the Apache License, Version 2.0.
4 // The license and further copyright text can be found in the file LICENSE.TXT at
5 // the root directory of the distribution.
6 // -----------------------------------------------------------------------
7 using System;
8 using System.Diagnostics;
9 using System.Globalization;
10 using System.IO;
11 using System.Runtime.Serialization.Formatters.Binary;
12 using System.Xml;
13 using log4net;
14 using Microsoft.Office.Core;
15 using Microsoft.Office.Interop.Word;
16 
17 namespace Chem4Word.Core
18 {
21  public static class ControlsStorage
22  {
23  private const string ControlsNodeName = "Controls";
24  private const string ControlsStorageNamespace = "urn:schemas-microsoft-com.VSTO2008Demos.ControlsStorage";
25  private const string RootNodeName = "ControlsStorage";
26  private static readonly ILog Log = LogManager.GetLogger(typeof (ControlsStorage));
27 
32  public static void Store(_Document document, ControlProperties[] controls)
33  {
34  Log.Debug(string.Format(CultureInfo.InvariantCulture, "ControlsStorage - Store : controls count {0}", controls.Length));
35  string xml = null;
36 
37  using (MemoryStream memStream = new MemoryStream())
38  {
39  BinaryFormatter formatter = new BinaryFormatter();
40  formatter.Serialize(memStream, controls);
41 
42  XmlDocument doc = new XmlDocument();
43  XmlElement root = doc.CreateElement(RootNodeName, ControlsStorageNamespace);
44  doc.AppendChild(root);
45 
46  XmlElement controlsDataNode = doc.CreateElement(ControlsNodeName, ControlsStorageNamespace);
47  controlsDataNode.InnerXml = Convert.ToBase64String(memStream.GetBuffer(), 0, (int) memStream.Length);
48  root.AppendChild(controlsDataNode);
49 
50  xml = doc.InnerXml;
51  }
52 
53  CustomXMLParts parts = document.CustomXMLParts.SelectByNamespace(ControlsStorageNamespace);
54  if (parts.Count > 0)
55  {
56  Debug.Assert(parts.Count == 1);
57  parts[1].Delete();
58  }
59 
60  document.CustomXMLParts.Add(xml, Type.Missing);
61  }
62 
67  public static ControlProperties[] Load(_Document document)
68  {
69  Log.Debug(string.Format(CultureInfo.InvariantCulture, "ControlsStorage - Load : document {0}", document));
70 
71  ControlProperties[] controls = null;
72  CustomXMLParts parts = document.CustomXMLParts.SelectByNamespace(ControlsStorageNamespace);
73 
74  if (parts != null && parts.Count > 0)
75  {
76  Debug.Assert(parts.Count == 1);
77  CustomXMLPart part = parts[1];
78  XmlDocument doc = new XmlDocument();
79  doc.LoadXml(part.XML);
80 
81  XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
82  nsmgr.AddNamespace("sc", ControlsStorageNamespace);
83 
84  XmlElement controlsElement =
85  doc.SelectSingleNode(String.Format(CultureInfo.CurrentUICulture, "//sc:{0}", ControlsNodeName),
86  nsmgr) as XmlElement;
87  if (controlsElement != null)
88  {
89  byte[] data = Convert.FromBase64String(controlsElement.InnerXml);
90  BinaryFormatter formatter = new BinaryFormatter();
91  controls = (ControlProperties[]) formatter.Deserialize(new MemoryStream(data));
92  }
93  }
94 
95  return controls;
96  }
97  }
98 }
Class that stores a list of property values for a saved control.
static readonly ILog Log
static void Store(_Document document, ControlProperties[] controls)
static ControlProperties [] Load(_Document document)