public static void SaveToFile(string filename, SbbInstructionSet inst) { using (var writer = new System.IO.StreamWriter(filename)) { var serializer = new XmlSerializer(inst.GetType()); serializer.Serialize(writer, inst); writer.Flush(); } }
public static SbbInstructionSet LoadFromFile(string filename) { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(GetInstructionSetSchema()); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings; XmlSchemaException firstException = null; settings.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) { logger.DebugFormat("Warning from Xml Validations: {0}", args.Message); } else { if (firstException == null) { firstException = args.Exception; } logger.Error(string.Format("Error from Xml Validation at Line {0} Pos {1}", args.Exception.LineNumber, args.Exception.LinePosition), args.Exception); } }; var res = new SbbInstructionSet(); using (var stream = System.IO.File.OpenRead(filename)) { using (XmlReader reader = XmlReader.Create(stream, settings)) { XmlSerializer ser = new XmlSerializer(res.GetType()); res = ser.Deserialize(reader) as SbbInstructionSet; } } if (firstException != null) { throw new ApplicationException(string.Format("Could not load instruction files due to XML validation error at Line {0} Position {1}: {2}", firstException.LineNumber, firstException.LinePosition, firstException.Message), firstException); } return(res); }