namespace Config.Web.Serialization {
	using System;
	using System.IO;
	using System.Xml.Serialization;
	using System.Text;

	public class ConfigSerialization {
		public static void PersistConfig(Configuration config,string filePath) {
			StreamWriter writer = null;
			XmlSerializer serializer;
			try {
				writer = File.CreateText(filePath);
				serializer = new XmlSerializer(typeof(Configuration));
				serializer.Serialize(writer,config);
			}
			finally {
				writer.Close();
			}
		}	
//装载web.config文件
		public static Configuration LoadConfig(string filePath) {
			StreamReader reader = null;
			XmlSerializer serializer;
			Configuration config;
			try {
				reader = new StreamReader(filePath);
				serializer = new XmlSerializer(typeof(Configuration));
				config = (Configuration)serializer.Deserialize(reader);
				return config;
			}
			finally {
				reader.Close();
			}
		}
	}

	/// <summary>
	/// Configuration 成员
	/// </summary>
	[XmlRoot(ElementName="configuration")]
	public class Configuration {
		SystemWeb _systemWeb;

		[XmlElement(ElementName="system.web")]
		public SystemWeb SystemWeb{
			get {
				return _systemWeb;
			}
			set {
				_systemWeb = value;
			}
		}
	}

	/// <summary>
	/// System.Web 成员
	/// </summary>
	public class SystemWeb {
		Compilation _compElem;
		CustomErrors _custErrors;
		Authentication _authElem;
		Trace _traceElem;
		Globalization _globalization;
		SessionState _sessionState;

		[XmlElement(ElementName="compilation")]
		public Compilation Compilation {
			get {
				return _compElem;
			}
			set {
				_compElem = value;
			}
		}

		[XmlElement(ElementName="customErrors")]
		public CustomErrors CustomErrors {
			get {
				return _custErrors;
			}
			set {
				_custErrors = value;
			}
		}

		[XmlElement(ElementName="authentication")]
		public Authentication Authentication {
			get {
				return _authElem;
			}
			set {
				_authElem = value;
			}
		}

		[XmlElement(ElementName="trace")]
		public Trace Trace {
			get {
				return _traceElem;
			}
			set {
				_traceElem = value;
			}
		}
		[XmlElement(ElementName="globalization")]
		public Globalization Globalization {
			get {
				return _globalization;
			}
			set {
				_globalization = value;
			}
		}
		[XmlElement(ElementName="sessionState")]
		public SessionState SessionState {
			get {
				return _sessionState;
			}
			set {
				_sessionState = value;
			}
		}
	}

	/// <summary>
	/// Compilation 成员
	/// </summary>
	public struct Compilation {
		string _defaultLanguage;
		bool _debug;

		[XmlAttribute(AttributeName="defaultLanguage")]
		public string DefaultLanguage {
			get {
				return _defaultLanguage;
			}
			set {
				_defaultLanguage = value;
			}
		}
		[XmlAttribute(AttributeName="debug")]
		public bool Debug {
			get {
				return _debug;
			}
			set {
				bool _debug = value;
			}
		}
	}

	/// <summary>
	/// CustomErrors 成员
	/// </summary>
	public struct CustomErrors {
		string _mode;

		[XmlAttribute(AttributeName="mode")]
		public string Mode {
			get {
				return _mode;
			}
			set {
				_mode = value;
			}
		}
	}

	/// <summary>
	/// Authentication 成员
	/// </summary>
	public struct Authentication {
		string _mode;

		[XmlAttribute(AttributeName="mode")]
		public string Mode {
			get {
				return _mode;
			}
			set {
				_mode = value;
			}
		}
	}

	/// <summary>
	/// Trace 成员
	/// </summary>
	public struct Trace {
		bool _enabled;
		int _requestLimit;
		bool _pageOutput;
		string _traceMode;

		[XmlAttribute(AttributeName="enabled")]
		public bool Enabled {
			get {
				return _enabled;
			}
			set {
				_enabled = value;
			}
		}
		[XmlAttribute(AttributeName="requestLimit")]
		public int RequestLimit {
			get {
				return _requestLimit;
			}
			set {
				_requestLimit = value;
			}
		}
		[XmlAttribute(AttributeName="pageOutput")]
		public bool PageOutput {
			get {
				return _pageOutput;
			}
			set {
				_pageOutput = value;
			}
		}
		[XmlAttribute(AttributeName="traceMode")]
		public string TraceMode {
			get {
				return _traceMode;
			}
			set {
				_traceMode = value;
			}
		}
	}

	/// <summary>
	/// SessionState 成员
	/// </summary>
	public struct SessionState {
		string _mode;
		string _stateConnectionString;
		string _sqlConnectionString;
		bool _cookieless;
		int _timeout;

		[XmlAttribute(AttributeName="mode")]
		public string Mode {
			get {
				return _mode;
			}
			set {
				_mode = value;
			}
		}
		[XmlAttribute(AttributeName="stateConnectionString")]
		public string StateConnectionString {
			get {
				return _stateConnectionString;
			}
			set {
				_stateConnectionString = value;
			}
		}
		[XmlAttribute(AttributeName="sqlConnectionString")]
		public string SqlConnectionString {
			get {
				return _sqlConnectionString;
			}
			set {
				_sqlConnectionString = value;
			}
		}
		[XmlAttribute(AttributeName="cookieless")]
		public bool Cookieless {
			get {
				return _cookieless;
			}
			set {
				_cookieless = value;
			}
		}
		[XmlAttribute(AttributeName="timeout")]
		public int Timeout {
			get {
				return _timeout;
			}
			set {
				_timeout = value;
			}
		}
	}

	/// <summary>
	/// Globalization 成员
	/// </summary>
	public struct Globalization {
		string _requestEncoding;
		string _responseEncoding;

		[XmlAttribute(AttributeName="requestEncoding")]
		public string RequestEncoding {
			get {
				return _requestEncoding;
			}
			set {
				_requestEncoding = value;
			}
		}
		[XmlAttribute(AttributeName="responseEncoding")]
		public string ResponseEncoding {
			get {
				return _responseEncoding;
			}
			set {
				_responseEncoding = value;
			}
		}
	}

}
