public void LoadOPC() { // Loading, clear existing nodes PeopleCollection.Clear(); try { // Use the default path and filename if none were provided if (string.IsNullOrEmpty(FullyQualifiedFilename)) { FullyQualifiedFilename = DefaultFullyQualifiedFilename; } string tempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), App.ApplicationFolderName); tempFolder = Path.Combine(tempFolder, App.AppDataFolderName + @"\"); OPCUtility.ExtractPackage(FullyQualifiedFilename, tempFolder); XmlSerializer xml = new XmlSerializer(typeof(People)); using (Stream stream = new FileStream(tempFolder + OPCContentFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { People people = (People)xml.Deserialize(stream); stream.Close(); foreach (Person person in people.PeopleCollection) { PeopleCollection.Add(person); } // 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 (Person person in PeopleCollection) { foreach (Relationship relationqhip in person.Relationships) { relationqhip.RelationTo = PeopleCollection.Find(relationqhip.PersonId); } } // Set the current person in the list CurrentPersonId = people.CurrentPersonId; CurrentPersonName = people.CurrentPersonName; PeopleCollection.Current = PeopleCollection.Find(CurrentPersonId); } PeopleCollection.IsDirty = false; return; } catch (Exception e) { // Could not load the file. Handle all exceptions // the same, ignore and continue. Debug.WriteLine(e.Message); 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.PeopleCollection == null || this.PeopleCollection.Count == 0) { return; } // Set the current person id and name before serializing this.CurrentPersonName = this.PeopleCollection.Current.FullName; this.CurrentPersonId = this.PeopleCollection.Current.Id; // Use the default path and filename if none was provided if (string.IsNullOrEmpty(this.FullyQualifiedFilename)) { this.FullyQualifiedFilename = People.DefaultFullyQualifiedFilename; } // Setup temp folders for this family to be packaged into OPC later string tempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), App.ApplicationFolderName); tempFolder = Path.Combine(tempFolder, App.AppDataFolderName); // Create the necessary directories Directory.CreateDirectory(tempFolder); // Create xml content file XmlSerializer xml = new XmlSerializer(typeof(People)); 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.PeopleCollection.IsDirty = false; }