コード例 #1
0
ファイル: People.cs プロジェクト: lgmorand/familytree
        public void LoadOPC()
        {
            // Loading, clear existing nodes
            this.PeopleCollection.Clear();

            try
            {
                // Use the default path and filename if none were provided
                if (string.IsNullOrEmpty(this.FullyQualifiedFilename))
                {
                    this.FullyQualifiedFilename = People.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 pc = (People)xml.Deserialize(stream);
                    stream.Close();

                    foreach (Person person in pc.PeopleCollection)
                    {
                        this.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 p in this.PeopleCollection)
                    {
                        foreach (Relationship r in p.Relationships)
                        {
                            r.RelationTo = this.PeopleCollection.Find(r.PersonId);
                        }
                    }

                    // Set the current person in the list
                    this.CurrentPersonId          = pc.CurrentPersonId;
                    this.CurrentPersonName        = pc.CurrentPersonName;
                    this.PeopleCollection.Current = this.PeopleCollection.Find(this.CurrentPersonId);
                }

                this.PeopleCollection.IsDirty = false;
                return;
            }
            catch
            {
                // Could not load the file. Handle all exceptions the same, ignore and continue.
                this.fullyQualifiedFilename = string.Empty;
            }
        }
コード例 #2
0
ファイル: People.cs プロジェクト: lgmorand/familytree
        /// <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;
        }