// deserialize an object from the given XML file public static AbstractExercise ReadExerciseFromXml(string fileName) { if (fileName == null) { throw new System.ArgumentNullException(); } // open XML file in FileStream FileStream xmlFile = new FileStream(fileName, FileMode.Open); // crete a text XML reader // associated it with the opened XML file above XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(xmlFile, new XmlDictionaryReaderQuotas()); // create a serializer for the AbstratExercise type DataContractSerializer serizalizer = new DataContractSerializer(typeof(AbstractExercise)); // Deserialize the object AbstractExercise exercise = (AbstractExercise)serizalizer.ReadObject(reader, true); // close streams reader.Close(); xmlFile.Close(); return(exercise); }
// serialize the object exercise into XML file by fileName public static void WriteExerciseToXml(AbstractExercise exercise, string fileName) { if (exercise == null || fileName == null) { throw new System.ArgumentNullException(); } // open the XML file in FileStream // overwrite the file if it exists, otherwise create a new one FileStream xmlFile = new FileStream(fileName, FileMode.Create); // create a text XML writer // associated it with the opened XML file stream above XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateTextWriter(xmlFile); // create a serializer for the AbstractExercise type DataContractSerializer serializer = new DataContractSerializer(typeof(AbstractExercise)); // serialize the exercise object serializer.WriteObject(xmlWriter, exercise); // close streams xmlWriter.Close(); xmlFile.Close(); }
protected override void DeleteThisExercise() { foreach (KeyValuePair <string, AbstractExercise> pair in NestedExercises_) { AbstractExercise.DeleteThis((AbstractExercise)pair.Value); } InvalidateThis(); }
// Equals (overload object.Equals()) // use the overriden GetHashCode() to test equality. public bool Equals(AbstractExercise oth) { TestValid(); if (null == oth) { return(false); } else { return(this.GetHashCode() == oth.GetHashCode()); } }
// delete an exercise in this album public bool DeleteExercise(string name) { TestValid(); if (name != null && NestedExercises_.ContainsKey(name)) { AbstractExercise ae = NestedExercises_[name]; AbstractExercise.DeleteThis(ae); NestedExercises_.Remove(name); return(true); } return(false); }
// get and return the exercise named exerciseName // nested in workDirectory public static AbstractExercise GetExercise(ExerciseAlbum workDirectory, string exerciseName) { // validate input arguments if (workDirectory == null || exerciseName == null || exerciseName == string.Empty) { throw new System.ArgumentException(); } // get exercise AbstractExercise exercise = workDirectory.GetExercise(exerciseName); // validate the result if (exercise == null) { throw new System.Exception( "this album does not contain such exercise album or exercise single!"); } return(exercise); }
// add // return true if success and vice versa public bool AddExercise(AbstractExercise exercise) { TestValid(); // validate input if (exercise == null || exercise.Name == string.Empty) { return(false); } // check duplicate exercise by name // throw an exception if a duplicate exists if (NestedExercises_.ContainsKey(exercise.Name)) { string ErrorInfo = "This album already has a(n) " + exercise.Name + "!"; throw new System.Exception(ErrorInfo); } // insert to the backing field //AbstractExercise newExercise = (AbstractExercise)exercise.Clone(); NestedExercises_.Add(exercise.Name, exercise); return(true); }
// load root album //private static string rootAlbum = "RootAlbum.xml"; public static ExerciseAlbum LoadRootExerciseAlbum(string rootAlbumPath) { return((ExerciseAlbum)AbstractExercise.ReadExerciseFromXml(rootAlbumPath)); }
// to call the correct version of DeleteThisExercise at run-time // based on the actual type of the object ae protected static void DeleteThis(AbstractExercise ae) { ae.DeleteThisExercise(); }