예제 #1
0
        /// De-Serializes an object and returns the result
        ///
        /// @param T the type of object to deserialize
        /// @param _filename the path at which to read the serialized object from
        public static T XmlDeserializeObject <T>(string _fileName)
        {
            // early out!
            if (string.IsNullOrWhiteSpace(_fileName))
            {
                return(default(T));
            }

            T objectOut = default(T);

            StringReader read = null;

            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(_fileName);
                string xmlString = xmlDocument.OuterXml;

                read = new StringReader(xmlString);
                Type outType = typeof(T);

                XmlSerializer serializer = new XmlSerializer(outType);

                XmlReader reader = new XmlTextReader(read);
                objectOut = (T)serializer.Deserialize(reader);
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (FileNotFoundException)
            {
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to deserialize!\n{e}");
            }
            finally
            {
                read?.Dispose();
            }
            return(objectOut);
        }
예제 #2
0
파일: Json.cs 프로젝트: gh201200/Game
 //释放
 public void Dispose()
 {
     json.Dispose();
     json = null;
 }