从别人那儿陶的一个配置文件处理方法.

public static class XmlHelper
	{
		private static byte[] XmlSerializeInternal(object o, Encoding encoding)
		{
			if( o == null )
				throw new ArgumentNullException("o");
			if( encoding == null )
				throw new ArgumentNullException("encoding");

			XmlSerializer ser = new XmlSerializer(o.GetType());
			using( MemoryStream ms = new MemoryStream() ) {
				using( XmlTextWriter writer = new XmlTextWriter(ms, encoding) ) {
					writer.Formatting = Formatting.Indented;
					ser.Serialize(writer, o);
					writer.Close();
				}
				return ms.ToArray();
			}
		}

		/// <summary>
		/// 将一个对象序列化为XML字符串
		/// </summary>
		/// <param name="o">要序列化的对象</param>
		/// <param name="encoding">编码方式</param>
		/// <returns>序列化产生的XML字符串</returns>
		public static string XmlSerialize(object o, Encoding encoding)
		{
			byte[] bytes = XmlSerializeInternal(o, encoding);
			return encoding.GetString(bytes);
		}
		
		/// <summary>
		/// 将一个对象按XML序列化的方式写入到一个文件
		/// </summary>
		/// <param name="o">要序列化的对象</param>
		/// <param name="path">保存文件路径</param>
		/// <param name="encoding">编码方式</param>
		public static void XmlSerializeToFile(object o, string path, Encoding encoding)
		{
			if( string.IsNullOrEmpty(path) )
				throw new ArgumentNullException("path");

			byte[] bytes = XmlSerializeInternal(o, encoding);
			File.WriteAllBytes(path, bytes);
		}

		/// <summary>
		/// 从XML字符串中反序列化对象
		/// </summary>
		/// <typeparam name="T">结果对象类型</typeparam>
		/// <param name="s">包含对象的XML字符串</param>
		/// <param name="encoding">编码方式</param>
		/// <returns>反序列化得到的对象</returns>
		public static T XmlDeserialize<T>(string s, Encoding encoding)
		{
			if( string.IsNullOrEmpty(s) )
				throw new ArgumentNullException("s");
			if( encoding == null )
				throw new ArgumentNullException("encoding");

			XmlSerializer mySerializer = new XmlSerializer(typeof(T));
			using( MemoryStream ms = new MemoryStream(encoding.GetBytes(s)) ) {
				using( StreamReader sr = new StreamReader(ms, encoding) ) {
					return (T)mySerializer.Deserialize(sr);
				}
			}
		}

		/// <summary>
		/// 读入一个文件,并按XML的方式反序列化对象。
		/// </summary>
		/// <typeparam name="T">结果对象类型</typeparam>
		/// <param name="path">文件路径</param>
		/// <param name="encoding">编码方式</param>
		/// <returns>反序列化得到的对象</returns>
		public static T XmlDeserializeFromFile<T>(string path, Encoding encoding)
		{
			if( string.IsNullOrEmpty(path) )
				throw new ArgumentNullException("path");
			if( encoding == null )
				throw new ArgumentNullException("encoding");

			string xml = File.ReadAllText(path, encoding);
			return XmlDeserialize<T>(xml, encoding);
		}


	}

 

public class MyCommand
    {
		[XmlAttribute("Name")]
		public string CommandName;

		[XmlAttribute]
		public string Database;

		[XmlArrayItem("Parameter")]
		public List<MyCommandParameter> Parameters = new List<MyCommandParameter>();

		[XmlElement]
		public MyCDATA CommandText;
    }
	
	public class MyCommandParameter
	{
		[XmlAttribute("Name")]
		public string ParamName;

		[XmlAttribute("Type")]
		public string ParamType;
	}

 

public class MyCDATA : IXmlSerializable
	{
		private string _value;

		public MyCDATA() { }

		public MyCDATA(string value)
		{
			this._value = value;
		}

		public string Value
		{
			get { return _value; }
		}

		XmlSchema IXmlSerializable.GetSchema()
		{
			return null;
		}

		void IXmlSerializable.ReadXml(XmlReader reader)
		{
			this._value = reader.ReadElementContentAsString();
		}

		void IXmlSerializable.WriteXml(XmlWriter writer)
		{
			writer.WriteCData(this._value);
		}

		public override string ToString()
		{
			return this._value;
		}

		public static implicit operator MyCDATA(string text)
		{
			return new MyCDATA(text);
		}
	}

 标记一下,慢慢研究.

posted @ 2011-12-31 09:06  darjuan  阅读(196)  评论(0编辑  收藏  举报