public void LoadOPC() { // Loading, clear existing nodes this.UMLCollection.Clear(); try { // Use the default path and filename if none were provided if (string.IsNullOrEmpty(this.FullyQualifiedFilename)) { this.FullyQualifiedFilename = UML.DefaultFullyQualifiedFilename; } string tempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), GlobalData.ApplicationFolderName); tempFolder = Path.Combine(tempFolder, GlobalData.AppDataFolderName + @"\"); OPCUtility.ExtractPackage(FullyQualifiedFilename, tempFolder); XmlSerializer xml = new XmlSerializer(typeof(UML)); using (Stream stream = new FileStream(tempFolder + OPCContentFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { UML pc = (UML)xml.Deserialize(stream); stream.Close(); foreach (Shape shape in pc.UMLCollection) { this.UMLCollection.Add(shape); } // To avoid circular references when serializing family data to xml, only the person Id // is seralized to express relationships. When family data is loaded, the correct // person object is found using the person Id and assigned to the appropriate relationship. foreach (Shape p in this.UMLCollection) { foreach (Relationship r in p.Relationships) { r.RelationTo = this.UMLCollection.Find(r.ShapeId); } } // Set the current person in the list this.CurrentShapeId = pc.CurrentShapeId; this.CurrentShapeName = pc.CurrentShapeName; this.UMLCollection.Current = this.UMLCollection.Find(this.CurrentShapeId); } this.UMLCollection.IsDirty = false; return; } catch { // Could not load the file. Handle all exceptions // the same, ignore and continue. this.fullyQualifiedFilename = string.Empty; } }
/// <summary> /// Persist the current list of people to disk. /// </summary> public void Save() { // Return right away if nothing to save. if (this.UMLCollection == null || this.UMLCollection.Count == 0) { return; } // Set the current person id and name before serializing this.CurrentShapeName = this.UMLCollection.Current.Name; this.CurrentShapeId = this.UMLCollection.Current.Id; // Use the default path and filename if none was provided if (string.IsNullOrEmpty(this.FullyQualifiedFilename)) { this.FullyQualifiedFilename = UML.DefaultFullyQualifiedFilename; } // Setup temp folders for this family to be packaged into OPC later string tempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), GlobalData.ApplicationFolderName); tempFolder = Path.Combine(tempFolder, GlobalData.AppDataFolderName); // Create the necessary directories Directory.CreateDirectory(tempFolder); // Create xml content file XmlSerializer xml = new XmlSerializer(typeof(UML)); using (Stream stream = new FileStream(Path.Combine(tempFolder, OPCContentFileName), FileMode.Create, FileAccess.Write, FileShare.None)) { xml.Serialize(stream, this); } // save to file package OPCUtility.CreatePackage(FullyQualifiedFilename, tempFolder); this.UMLCollection.IsDirty = false; }