public bool Open(string path)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                memStream = new MemoryStream();
                memStream.Write(buffer, 0, (int)fs.Length);
                buffer = null;
                fs.Close();

                package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);

                docParts = new Dictionary<string, DocumentPart>();
                PackagePartCollection parts = package.GetParts();
                foreach (PackagePart part in parts)
                {
                    DocumentPart docPart = new DocumentPart(part);
                    docParts.Add(part.Uri.ToString(), docPart);
                }

                filePath = path;
                dirty = false;
                                
                return true;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return false;
            }
        }
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (!(e.Node.Tag is DocumentPart))
            {
                return;
            }

            DocumentPart docPart = e.Node.Tag as DocumentPart;

            this.comboBoxRelType.Items.Clear();

            List <PartInfo> list = KnownPackageInfo.GetPartTypesForContentType(docPart.ContentType);

            if (list == null || list.Count == 0)
            {
                foreach (string relType in KnownPackageInfo.GetRelationshipTypes().Keys)
                {
                    this.comboBoxRelType.Items.Add(relType);
                }
                return;
            }

            foreach (PartInfo partInfo in list)
            {
                this.comboBoxRelType.Items.Add(partInfo.m_relType);
            }
            comboBoxRelType.Text = list[0].m_relType;
        }
        public bool Open(string path)
        {
            try
            {
                FileStream fs     = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                memStream = new MemoryStream();
                memStream.Write(buffer, 0, (int)fs.Length);
                buffer = null;
                fs.Close();

                package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);

                docParts = new Dictionary <string, DocumentPart>();
                PackagePartCollection parts = package.GetParts();
                foreach (PackagePart part in parts)
                {
                    DocumentPart docPart = new DocumentPart(part);
                    docParts.Add(part.Uri.ToString(), docPart);
                }

                filePath = path;
                dirty    = false;

                return(true);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return(false);
            }
        }
        public void ReloadFromMemory()
        {
            Debug.Assert(memStream != null);

            this.docParts = null;
            package.Close();
            package = null;
            package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);

            docParts = new Dictionary <string, DocumentPart>();
            PackagePartCollection parts = package.GetParts();

            foreach (PackagePart part in parts)
            {
                DocumentPart docPart = new DocumentPart(part);
                docParts.Add(part.Uri.ToString(), docPart);
            }
        }
        public void ReloadFromMemory()
        {
            Debug.Assert(memStream != null);

            this.docParts = null;
            package.Close();
            package = null;
            package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);

            docParts = new Dictionary<string, DocumentPart>();
            PackagePartCollection parts = package.GetParts();
            foreach (PackagePart part in parts)
            {
                DocumentPart docPart = new DocumentPart(part);
                docParts.Add(part.Uri.ToString(), docPart);
            }
        }
        private void OpenPartEditor(DocumentPart part)
        {
            uint editorFlags = (uint)__VSSPECIFICEDITORFLAGS.VSSPECIFICEDITOR_UseEditor | (uint)__VSSPECIFICEDITORFLAGS.VSSPECIFICEDITOR_DoOpen;
            Guid editorGuid = GuidList.guidPartEditorFactory;
            string physicalView = part.Path; // unique name of xml fragment (physical view must be a unique name).
            Guid logicalViewGuid = VSConstants.LOGVIEWID_Primary;

            string ext = System.IO.Path.GetExtension(physicalView);
            if (!KnownPackageInfo.IsXml(ext) && !KnownPackageInfo.IsText(ext))
            {
                if (MessageBox.Show(
                    this,
                    "Do you want to export the part to a file and then edit it?",
                    AboutForm.AssemblyTitle,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string fileName = ExportPart();
                    if (string.IsNullOrEmpty(fileName))
                        return;

                    VsShellUtilities.OpenDocument(vsServiceProvider, fileName);
                }

                return;
            }

            IntPtr docDataExisting = Marshal.GetIUnknownForObject(this);
            IVsWindowFrame windowFrame;

            IVsHierarchy hierarchy;
            uint itemIdFindAndLock;
            IntPtr docDataFindAndLock;
            uint docCookieFindAndLock;

            int hr = runningDocTable.FindAndLockDocument(
                (uint)_VSRDTFLAGS.RDT_NoLock,
                docPkg.FilePath,
                out hierarchy,
                out itemIdFindAndLock,
                out docDataFindAndLock,
                out docCookieFindAndLock
            );
            if (hr != VSConstants.S_OK)
            {
                Debug.Assert(false, "Couldn't find the current doc!");
                return;
            }

            IVsProject3 project = hierarchy as IVsProject3;

            hr = project.OpenItemWithSpecific(
                itemIdFindAndLock,
                editorFlags,
                ref editorGuid,
                physicalView,
                ref logicalViewGuid,
                docDataExisting,
                out windowFrame);
            if (hr != VSConstants.S_OK)
            {
                Debug.Assert(false, "Couldn't open the part!");
                return;
            }

            Marshal.Release(docDataExisting);

            if (windowFrame != null)
            {
                // Register the window events to a view helper
                PartViewHelper windowFrameEventsHandler = new PartViewHelper(this, windowFrame);
                ErrorHandler.ThrowOnFailure(windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_ViewHelper, (IVsWindowFrameNotify3)windowFrameEventsHandler));

                windowFrame.Show();
            }
        }