学习序列化

应用程序启动时,加载配置文件,有两种方式解析XML,一种是XPATH,一种是用序列化,下面是用序列化:
http://www.yesky.com/20030218/1652674.shtml

  1class SerializerHelper
  2    {
  3        /**//// <summary>
  4        /// 序列化:写文件,将对象序列化成文件格式的文本,这里是XML
  5        /// </summary>
  6        /// <param name="obj">要序列化的对象</param>
  7        /// <param name="filePath">文件路径</param>

  8        public void SerializeObject(object obj,string filePath)
  9        {
 10            TextWriter writer = new StreamWriter(filePath);
 11            XmlSerializer xmlSer = new XmlSerializer(obj.GetType());
 12            xmlSer.Serialize(writer, obj);
 13            writer.Close();
 14        }

 15
 16        /**//// <summary>
 17        /// 反序列化:读文件,将文件格式的文本(这里是XML)反序列化成对象
 18        /// </summary>
 19        /// <param name="t">对象类型</param>
 20        /// <param name="filePath">文件路径</param>
 21        /// <returns></returns>

 22        public object DeserializeObject(Type t, string filePath)
 23        {
 24            TextReader reader = new StreamReader(filePath);
 25            XmlSerializer xmlSer = new XmlSerializer(t);
 26            return xmlSer.Deserialize(reader);
 27        }

 28
 29        /**//// <summary>
 30        /// 反序列化:读文件,将文件格式的文本(这里是XML)反序列化成对象
 31        /// </summary>
 32        /// <typeparam name="TObject">范型,指所有类型</typeparam>
 33        /// <param name="filePath">文件路径</param>
 34        /// <returns></returns>

 35        public TObject DeserializeObject<TObject>(string filePath)
 36        {
 37            TextReader reader = new StreamReader(filePath);
 38            XmlSerializer xmlSer = new XmlSerializer(typeof(TObject));
 39            return (TObject)xmlSer.Deserialize(reader);
 40        }

 41    }

 42
 43public class SettingsHelper
 44    {
 45        SettingsItem 类#region SettingsItem 类
 46        [Serializable()]
 47        public class SettingsItem
 48        {
 49            Constructor#region Constructor
 50            /**//// <summary>
 51            /// 
 52            /// </summary>

 53            public SettingsItem()
 54            {
 55            }

 56            #endregion

 57
 58            Fields#region Fields
 59            string m_PluginXmlFileName;
 60            string m_WebServiceConfiguration;
 61            string m_WorkDir;
 62            string m_TmpDir;
 63            string m_DecimalFormatString;
 64            string m_DepartmentTitle;
 65            string m_ApplicationTitle;
 66            bool m_IsSingleApplication;
 67            string m_BudgetYear;
 68            string m_LastUserId;
 69            string m_SkinName;
 70            bool m_AutoUpgrade;
 71            bool m_Setting;
 72            int m_Style;
 73            #endregion

 74
 75            Properties#region Properties
 76            /**//// <summary>
 77            /// PluginConfig 配置文件
 78            /// </summary>

 79            public string PluginXmlFileName
 80            {
 81                get return m_PluginXmlFileName; }
 82                set { m_PluginXmlFileName = value; }
 83            }

 84
 85            /**//// <summary>
 86            /// WebService 客户端配置文件
 87            /// </summary>

 88            public string WebServiceConfiguration
 89            {
 90                get return m_WebServiceConfiguration; }
 91                set { m_WebServiceConfiguration = value; }
 92            }

 93
 94            /**//// <summary>
 95            /// WorkDir 名称
 96            /// </summary>

 97            public string WorkDir
 98            {
 99                get return m_WorkDir; }
100                set { m_WorkDir = value; }
101            }

102
103            /**//// <summary>
104            /// TmpDir 名称
105            /// </summary>

106            public string TmpDir
107            {
108                get return m_TmpDir; }
109                set { m_TmpDir = value; }
110            }

111
112            /**//// <summary>
113            /// Decimal 数据的格式
114            /// </summary>

115            public string DecimalFormatString
116            {
117                get return m_DecimalFormatString; }
118                set { m_DecimalFormatString = value; }
119            }

120
121            /**//// <summary>
122            /// 基础资料树中查看详细信息查看名称
123            /// </summary>

124            public string DepartmentTitle
125            {
126                get return m_DepartmentTitle; }
127                set { m_DepartmentTitle = value; }
128            }

129
130            /**//// <summary>
131            /// 应用程序的标题
132            /// </summary>

133            public string ApplicationTitle
134            {
135                get return m_ApplicationTitle; }
136                set { m_ApplicationTitle = value; }
137            }

138
139            /**//// <summary>
140            /// 是否只运行单个应用程序
141            /// </summary>

142            public bool IsSingleApplication
143            {
144                get return m_IsSingleApplication; }
145                set { m_IsSingleApplication = value; }
146            }

147
148            /**//// <summary>
149            /// 基础资料树中查看详细信息查看名称
150            /// </summary>

151            public string BudgetYear
152            {
153                get return m_BudgetYear; }
154                set { m_BudgetYear = value; }
155            }

156
157            /**//// <summary>
158            /// 当前登录的用户ID
159            /// </summary>

160            public string LastUserId
161            {
162                get return m_LastUserId; }
163                set { m_LastUserId = value; }
164            }

165
166            /**//// <summary>
167            /// 当Style=4时的样式名称
168            /// 例:Stardust代表梦幻,Money Twins代表货币,The Asphalt World代表清爽,
169            /// Caramel代表怀旧,Liquid Sky代表湖蓝,Coffee代表咖啡,Glass Oceans代表玻璃
170            /// </summary>

171            public string SkinName
172            {
173                get return m_SkinName; }
174                set { m_SkinName = value; }
175            }

176
177            /**//// <summary>
178            /// 应用程序启动时,是否自动更新
179            /// </summary>

180            public bool AutoUpgrade
181            {
182                get return m_AutoUpgrade; }
183                set { m_AutoUpgrade = value; }
184            }

185
186            /**//// <summary>
187            /// 
188            /// </summary>

189            public bool Setting
190            {
191                get return m_Setting; }
192                set { m_Setting = value; }
193            }

194
195            /**//// <summary>
196            /// 样式,例:0代表扁平,1代表超扁平,2代表经典,3代表Office2003,4代表皮肤
197            /// </summary>

198            public int Style
199            {
200                get return m_Style; }
201                set { m_Style = value; }
202            }

203            #endregion

204        }

205        #endregion

206
207        void Deserialize()
208        {
209            SerializerHelper ser = new SerializerHelper();
210            //SettingsItem set = ser.DeserializeObject<SettingsItem>(@"ApplicationConfig\TestConfig.xml");
211            SettingsItem set = (SettingsItem)ser.DeserializeObject(typeof(SettingsItem), @"ApplicationConfig\TestConfig.xml");
212            
213            Console.WriteLine(set.ApplicationTitle);
214            Console.WriteLine(set.DepartmentTitle);
215            Console.WriteLine(set.IsSingleApplication);
216        }

217
218        void Serialize()
219        {
220            SerializerHelper ser = new SerializerHelper();
221            SettingsItem set = new SettingsItem();
222            set.ApplicationTitle = "dengqian";
223            set.AutoUpgrade = true;
224            set.BudgetYear = "2008";
225            set.DecimalFormatString = "######0000";
226            set.DepartmentTitle = "预算年份";
227            ser.SerializeObject(set@"ApplicationConfig\TestConfig.xml");
228
229            Console.WriteLine(set.ApplicationTitle);
230            Console.WriteLine(set.DepartmentTitle);
231            Console.WriteLine(set.IsSingleApplication);
232        }

233
234        public static void main()
235        {
236            SettingsHelper helper = new SettingsHelper();
237            helper.Serialize();
238            Console.WriteLine("-----------------------");
239            helper.Deserialize();
240        }

241    }

转载于:https://www.cnblogs.com/pretty/archive/2008/01/15/1039563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值