读取/保存xml文件的类(序列化/反序列化)

  1 None.gif
  2 None.gif using  System;
  3 None.gif using  System.Collections.Generic;
  4 None.gif using  System.Text;
  5 None.gif using  System.Xml.Serialization;
  6 None.gif using  System.IO;
  7 None.gif using  System.Reflection;
  8 None.gif
  9 None.gif namespace  Mobile.Services
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 12InBlock.gif    /// 读取/保存xml文件的类
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    public class XmlService
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        private string _strPath;
 17InBlock.gif
 18InBlock.gif        public XmlService()
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            //this._strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase + "\\SysSetting.xml");
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23InBlock.gif
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 25InBlock.gif        /// 持久化文件路径
 26ExpandedSubBlockEnd.gif        /// </summary>

 27InBlock.gif        public string XMLPath
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _strPath; }
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _strPath = value; }
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33ContractedSubBlock.gifExpandedSubBlockStart.gif        公有方法/属性#region 公有方法/属性
 34InBlock.gif
 35InBlock.gif
 36ContractedSubBlock.gifExpandedSubBlockStart.gif        Serialize 重载#region Serialize 重载
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 38InBlock.gif        /// 根据object对象,在默认xml存储路径生成记录已往搜索条件的集合的xml文件
 39InBlock.gif        /// </summary>
 40InBlock.gif        /// <param name="serializeObj">待转换的object对象</param>
 41ExpandedSubBlockEnd.gif        /// <param name="type">要序列化的对象的类型</param>

 42InBlock.gif        private void Serialize(object serializeObj, Type type)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            //直接序列化生成xml
 45InBlock.gif            XmlSerializer serializer = new XmlSerializer(type);
 46InBlock.gif            TextWriter writer = new StreamWriter(XMLPath);
 47InBlock.gif            serializer.Serialize(writer, serializeObj);
 48InBlock.gif            writer.Close();
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 52InBlock.gif        /// 根据object对象,在默认xml存储路径生成记录已往搜索条件的集合的xml文件
 53InBlock.gif        /// </summary>
 54InBlock.gif        /// <param name="strPath">指定xml存储路径</param>
 55InBlock.gif        /// <param name="type">要序列化的对象的类型</param>
 56ExpandedSubBlockEnd.gif        /// <param name="serializeObj">待转换的object对象</param>

 57InBlock.gif        public void Serialize(object serializeObj, Type type, string strPath)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            //直接序列化生成xml
 60InBlock.gif            XmlSerializer serializer = new XmlSerializer(type);
 61InBlock.gif            TextWriter writer = new StreamWriter(strPath);
 62InBlock.gif
 63InBlock.gif            serializer.Serialize(writer, serializeObj);
 64InBlock.gif            writer.Close();
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67ExpandedSubBlockEnd.gif        #endregion
 //Serialize
 68InBlock.gif
 69ContractedSubBlock.gifExpandedSubBlockStart.gif        Deserialize 重载#region Deserialize 重载
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 71InBlock.gif        /// 从默认的路径获取xml文件,根据xml文件生成对应object对象
 72InBlock.gif        /// </summary>
 73InBlock.gif        /// <param name="type"></param>
 74ExpandedSubBlockEnd.gif        /// <returns>对应object对象</returns>

 75InBlock.gif        private object Deserialize(Type type)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            object ras = this.Deserialize(this.XMLPath, type);
 78InBlock.gif            return ras;
 79ExpandedSubBlockEnd.gif        }

 80InBlock.gif
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 82InBlock.gif        /// 从指定路径获取xml文件,根据xml文件生成对应object对象
 83InBlock.gif        /// </summary>
 84InBlock.gif        /// <param name="strPath">指定路径</param>
 85InBlock.gif        /// <param name="type">反序列化的对象类型</param>
 86ExpandedSubBlockEnd.gif        /// <returns>对应object对象</returns>

 87InBlock.gif        public object Deserialize(string strPath, Type type)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            object ras = new object();
 90InBlock.gif            try
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                // 判断文件是否存在
 93InBlock.gif                if (File.Exists(strPath))
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 95InBlock.gif                    TextReader reader = new StreamReader(strPath, Encoding.UTF8);
 96InBlock.gif                    XmlSerializer serializer = new XmlSerializer(type);
 97InBlock.gif                    ras = (object)serializer.Deserialize(reader);
 98InBlock.gif
 99InBlock.gif                    reader.Close();
100ExpandedSubBlockEnd.gif                }

101ExpandedSubBlockEnd.gif            }

102InBlock.gif            catch (Exception err)
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                throw err;
105ExpandedSubBlockEnd.gif            }

106InBlock.gif            return ras;
107ExpandedSubBlockEnd.gif        }

108ExpandedSubBlockEnd.gif        #endregion
 //Deserialize重载
109InBlock.gif
110InBlock.gif
111ExpandedSubBlockEnd.gif        #endregion

112InBlock.gif
113ExpandedSubBlockEnd.gif    }

114ExpandedBlockEnd.gif}

115 None.gif

转载于:https://www.cnblogs.com/wt0731/archive/2007/08/12/852442.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值