使用Xml序列化和反序列化

添加人:iyond六级(3302分)   添加时间:2007-12-04    阅读次数:1004  收藏此教程
以前写的Xml序列化和反序列化的代码,希望对大家有些帮助
 
  1using System;
  2using System.IO;
  3using System.Xml;
  4using System.Xml.Serialization;
  5using System.Collections;
  6using System.Web;
  7using System.Web.Caching;
  8using System.Text;
  9using System.Configuration;
 10
 11namespace Cutech.AddressBook.Configuration
 12{
 13    public class ModuleConfig
 14    {
 15        public static ModuleSettings GetSettings()
 16        {
 17            HttpContext context = HttpContext.Current;
 18            ModuleSettings data = (ModuleSettings)context.Cache["AddressBook_Settings"];
 19            if (data == null)
 20            {
 21                XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings));
 22                try
 23                {
 24                    string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
 25                    // create a filestream to read the XML document
 26                    FileStream fs = new FileStream(fileName, FileMode.Open);
 27                    // use the Deserialize method to retrieve the oject state
 28                    data = (ModuleSettings)serializer.Deserialize(fs);
 29                    fs.Close();
 30
 31                    context.Cache.Insert("AddressBook_Settings", data, new CacheDependency(fileName));
 32                }

 33                catch (System.IO.FileNotFoundException)
 34                {
 35                    // if the file is not found, return a new empty class
 36                    data = new ModuleSettings();
 37                }

 38            }

 39
 40            return data;
 41        }

 42
 43        public static void SaveSettings(ModuleSettings data)
 44        {
 45            string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
 46            XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings));
 47
 48            // serialize the object
 49            FileStream fs = new FileStream(fileName, FileMode.Create);
 50            serializer.Serialize(fs, data);
 51            fs.Close();
 52        }

 53
 54        private static string GetSettingsFile()
 55        {
 56            HttpContext context = HttpContext.Current;
 57            // get the file path from the cache
 58            string filePath = (string)context.Cache["AddressBook_SettingsFile"];
 59            // if path is null, get it from web.config
 60            if (filePath == null)
 61            {
 62                // retrieve the value
 63                filePath = ConfigurationSettings.AppSettings["AddressBook_SettingsFile"];
 64                // save into the cache
 65                context.Cache["AddressBook_SettingsFile"] = filePath;
 66            }

 67            // return the path
 68            return filePath;
 69        }

 70    }

 71
 72    [Serializable()]
 73    public class ModuleSettings
 74    {
 75        private string connectionString;
 76        private string strMoneyUrl;
 77        private string strLoginUrl;
 78        private string strServiceID;
 79
 80        public string ConnectionString
 81        {
 82            get { return connectionString; }
 83            set { connectionString = value; }
 84        }

 85        public string MoneyUrl
 86        {
 87            get { return strMoneyUrl; }
 88            set { strMoneyUrl = value; }
 89        }

 90        public string LoginUrl
 91        {
 92            get { return strLoginUrl; }
 93            set { strLoginUrl = value; }
 94        }

 95        public string ServiceID
 96        {
 97            get { return strServiceID; }
 98            set { strServiceID = value; }
 99        }

100    }

101}
1页 第1上一页1下一页
相关的教程: XML 序列化 反序列化
收藏此教程

当前平均分: 0.0(0 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: