using System.IO;
using System.Xml.Linq;

namespace MECF.Framework.Common.Account.Extends
{
	/// <summary>
	/// Xml文件加载器。
	/// </summary>
	public abstract class XmlLoader
	{

		#region Variables

		/// <summary>
		/// Xml文件名。
		/// </summary>
		protected readonly string FileName;
		
		/// <summary>
		/// Xml文件加载后的XDocument对象。
		/// </summary>
		protected XDocument XmlDoc;

		#endregion

		#region Constructors

		/// <summary>
		/// 创建Xml文件加载器示例。
		/// </summary>
		/// <param name="fileName"></param>
		protected XmlLoader(string fileName)
		{
			FileName = fileName;
		}

		#endregion

		#region Methods
        
		/// <summary>
		/// 加载Xml文件。
		/// </summary>
		/// <exception cref="FileNotFoundException"></exception>
		public virtual void Load()
		{
			if (File.Exists(FileName))
			{
				XmlDoc = XDocument.Load(FileName);
				AnalyzeXml();
				return;
			}
			
			throw new FileNotFoundException("File " + FileName + " not be found");
		}
		
		/// <summary>
		/// 解析Xml文件。
		/// </summary>
		protected virtual void AnalyzeXml()
		{
		}
		
		#endregion
	}
}
