object o = MyConfigClass.DeSerializeObject("xyz", typeof(MyOtherClass), false);~~Bonnie
> >/// <summary> > /// Deserializes an object from file and returns a reference. > /// </summary> > /// <param name="Filename">name of the file to serialize to</param> > /// <param name="ObjectType">The Type of the object. Use typeof(yourobject class)</param> > /// <param name="BinarySerialization">determines whether we use Xml or Binary serialization</param> > /// <returns>Instance of the deserialized object or null. Must be cast to your object type</returns> > public static object DeSerializeObject(string Filename,Type ObjectType,bool BinarySerialization) > { > object Instance = null; > > if (!BinarySerialization) > { > > XmlReader reader = null; > XmlSerializer serializer = null; > FileStream fs = null; > try > { > // Create an instance of the XmlSerializer specifying type and namespace. > serializer = new XmlSerializer(ObjectType); > > // A FileStream is needed to read the XML document. > fs = new FileStream(Filename, FileMode.Open); > reader = new XmlTextReader(fs); > > Instance = serializer.Deserialize(reader); > > } > catch > { > return null; > } > finally > { > if (fs != null) > fs.Close(); > > if (reader != null) > reader.Close(); > } > } > else > { > > BinaryFormatter serializer = null; > FileStream fs = null; > > try > { > serializer = new BinaryFormatter(); > fs = new FileStream(Filename, FileMode.Open); > Instance = serializer.Deserialize(fs); > > } > catch > { > return null; > } > finally > { > if (fs != null) > fs.Close(); > } > } > > return Instance; > } > > >