public void WriteGuy(Guy guyToWrite) { GuyFile = Path.GetFullPath(guyToWrite.Name + ".xml"); if (File.Exists(GuyFile)) { File.Delete(GuyFile); } using (Stream outputStream = File.OpenWrite(GuyFile)) { DataContractSerializer serializer = new DataContractSerializer(typeof(Guy)); serializer.WriteObject(outputStream, guyToWrite); } OnPropertyChanged("GuyFile"); }
public void ReadGuy() { if (string.IsNullOrEmpty(GuyFile)) { return; } // If file does not exist, an IO exception occurs here. using (Stream inputStream = File.OpenRead(GuyFile)) { DataContractSerializer serializer = new DataContractSerializer(typeof(Guy)); // If invalid XML, such as a string in a field expected to be an integer, or an invalid enum, a serializer exception occurs here noting that. NewGuy = serializer.ReadObject(inputStream) as Guy; } OnPropertyChanged("NewGuy"); }