日期:2014-05-20 浏览次数:20906 次
public static T Load<T>(string filePath)
            where T:class
        {
            FileStream fs = null;
            try
            {
                if (!File.Exists(filePath))
                {
                    return null;
                }
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var serializater = new XmlSerializer(typeof(T));
                return serializater.Deserialize(fs) as T;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
------解决方案--------------------
XML序列化
 public static bool Save<T>(T entity,string filePath)
        {
            var success = false;
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                var xmlWriter = new XmlSerializer(typeof(T));
                xmlWriter.Serialize(fs, entity);
                success = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return success;
        }