public static IOperation[] loadOpsFile(string path, Type[] knownTypes) { //Open the operation file. FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); //Read in the file and store each item to a list. Type is "anyType" but this will be cast to IOperation. DataContractSerializer serializer = new DataContractSerializer(typeof(IOperation), knownTypes); XmlReader read = XmlReader.Create(fs); read.ReadToDescendant("z:anyType"); List<IOperation> opList = new List<IOperation>(); //Blurahegle while (serializer.IsStartObject(read)) { //Check each type when deserializing. Make sure we can cast it. try { opList.Add((IOperation)serializer.ReadObject(read)); } catch (Exception e) { MessageBox.Show("Invalid operation type encountered. Please ensure all required libraies are installed \n" + e.Message, "An error has occured",MessageBoxButtons.OK,MessageBoxIcon.Error); fs.Close(); return null; } } fs.Close(); return opList.ToArray(); //Done }
public static List< Hero > DeserializeHeroList( string combinedHeroXmlFile ) { var heroSerializer = new DataContractSerializer( typeof( List<Hero> ) ); using( var dataFileStream = new FileStream( combinedHeroXmlFile, FileMode.Open ) ) { var dictReader = XmlDictionaryReader.CreateTextReader( dataFileStream, new XmlDictionaryReaderQuotas() ); while( dictReader.Read() ) { switch( dictReader.NodeType ) { case XmlNodeType.Element: if( heroSerializer.IsStartObject( dictReader ) ) { return (List<Hero>)heroSerializer.ReadObject( dictReader ); } break; } } } return null; }
public static Statistics Load() { Statistics statistics = null; try { // TODO : This is not to be used for this release -- still some pondering to do. return new Statistics(); DataContractSerializer serializer = new DataContractSerializer(typeof(Statistics), new Type[] { }); using (FileStream fs = new FileStream(Statistics.Filename, FileMode.Open)) { using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas())) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (serializer.IsStartObject(reader)) { statistics = (Statistics)serializer.ReadObject(reader); } break; } } } } //using (StreamWriter swStatistics = new StreamWriter(Statistics.Filename)) //{ // using (XmlTextWriter writer = new XmlTextWriter(swStatistics)) // { // writer.Formatting = Formatting.Indented; // serializer.WriteObject(writer, this); // writer.Flush(); // } //} //XmlSerializer mySerializer = new XmlSerializer(typeof(Statistics), new Type[] { }); //FileStream myFileStream = new FileStream(Statistics.Filename, FileMode.Open); //statistics = (Statistics)mySerializer.Deserialize(myFileStream); } catch { statistics = new Statistics(); } return statistics; }