⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlconfig.cs

📁 报刊广告管理系统。CSharp编写
💻 CS
字号:
using System;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace WWAM
{
	/// <summary>
	/// 应用程序配置类
	/// </summary>
	public class XMLConfig
	{
		static string defaultFileName = Application.StartupPath + @"\Config.xml";
		static string defaultMainNode = "Config";
		
		string fileName = defaultFileName;
		string mainNode = defaultMainNode;
		public XMLConfig()
		{
		}

		public XMLConfig(string fileName)
		{
			FileName = fileName;			
		}

		public XMLConfig(string fileName , string mainNode)
		{
			FileName = fileName;
			MainNode = mainNode;
		}

		#region Class Property&Method
		public string FileName
		{
			get
			{
				return fileName;
			}
			set
			{
				fileName = value;
				checkFileName(ref fileName,ref mainNode);
			}
		}
		public string MainNode
		{
			get
			{
				return mainNode;
			}
			set
			{
				mainNode = value;
				checkFileName(ref fileName,ref mainNode);
			}
		}

		public string this[string node]
		{
			get
			{
				return GetNodeValue(fileName,mainNode,node);
			}
			set
			{
				SetNode(fileName,mainNode,node,value);
			}
		}

		public void Remove(string node)
		{
			RemoveNode(fileName,mainNode,node);
		}

		public XmlNodeList Nodes
		{
			get
			{
				return GetNodes(fileName,mainNode);
			}
		}

		public void RemoveAll()
		{
			RemoveAllNodes(fileName,mainNode);
		}

		public string Content
		{
			get
			{
				return GetContent(fileName,mainNode);
			}
		}

		#endregion

		#region Static Method 
		#region Get && Set
		private static void checkFileName(ref string fileName, ref string mainNode)
		{
			try
			{
				if(Path.GetExtension(fileName)!=".xml")
					fileName = Path.ChangeExtension(fileName,".xml");
				if(!Char.IsLetter(mainNode[0])&&mainNode[0]!='_')
					mainNode = '_'+ mainNode;
				if(!File.Exists(fileName))
				{
					XmlWriter wr = new XmlTextWriter(File.Create(fileName),System.Text.Encoding.UTF8);
					wr.WriteStartDocument();
					wr.WriteStartElement(mainNode);
					wr.WriteEndDocument();
					wr.Flush();
					wr.Close();
				}
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message + ex.StackTrace);
			}

		}

		public static void SetNode(string fileName, string mainNode, string node , string val)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return ;
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			checkFileName(ref fileName,ref mainNode);
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				if(doc[mainNode]==null)
				{
					XmlNode xmlmainNode = doc.CreateNode(XmlNodeType.Element,mainNode,"");
					doc.AppendChild(xmlmainNode);
				}
				if(doc[mainNode][node]!=null)
				{
					doc[mainNode][node].InnerText = val;
				}
				else
				{
					XmlNode xmlnode = doc.CreateNode(XmlNodeType.Element,node,"");
					xmlnode.InnerText = val;
					doc[mainNode].AppendChild(xmlnode);

				}
				doc.Save(fileName);
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message + ex.StackTrace);
			}			
		}

		public static XmlNode SetNewNode(string fileName, string mainNode, string node , string val)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return null;
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			checkFileName(ref fileName,ref mainNode);
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				if(doc[mainNode]==null)
				{
					XmlNode xmlmainNode = doc.CreateNode(XmlNodeType.Element,mainNode,"");
					doc.AppendChild(xmlmainNode);
				}
				XmlNode xmlnode = doc.CreateNode(XmlNodeType.Element,node,"");
				xmlnode.InnerText = val;
				doc[mainNode].AppendChild(xmlnode);

				doc.Save(fileName);
				return xmlnode;
			}
			catch//(Exception ex)
			{
				return null;
			}			
		}

		public static XmlNode GetNode(string fileName, string mainNode, string node)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return null;
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				return doc[mainNode][node];
			}
			catch
			{
				return null;
			}
		}
		public static string GetNodeValue(string fileName, string mainNode, string node)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return "";
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				if(doc[mainNode][node]==null)
				{
					return "";
				}
				else
				{
					return doc[mainNode][node].InnerText;
				}
			}
			catch
			{
				return "";
			}
		}
		public static string GetNoteValueWithDefault(string filename, string mainNode, string node, string defaultValue)
		{
			string str = GetNodeValue(filename,mainNode,node);
			if(str=="")
			{
				SetNode(filename,mainNode,node,defaultValue);
				return defaultValue;
			}
			else
			{
				return str;
			}
		}
		public static void RemoveNode(string fileName, string mainNode, string node)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return ;
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				doc[mainNode].RemoveChild(doc[mainNode][node]);
				doc.Save(fileName);
			}
			catch
			{
			}
		}
		public static XmlNodeList GetNodes(string fileName, string mainNode)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0)
			{
				return null;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				if(doc[mainNode]!=null)
				{
					return doc[mainNode].ChildNodes;
				}
				else
				{
					return null;
				}
			}
			catch
			{
				return null;
			}
		}
		public static void RemoveAllNodes(string fileName, string mainNode)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0)
			{
				return ;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				if(doc[mainNode]!=null)
				{
					doc[mainNode].RemoveAll();
				}
			}
			catch
			{
			}

		}
		public static string GetContent(string fileName, string mainNode)
		{
			try
			{
				XmlTextReader rd = new XmlTextReader(fileName);
				rd.MoveToContent();
				string s = rd.ReadOuterXml();
				rd.Close();
				return s;
			}
			catch
			{
				return "";
			}
		}
		#endregion
		#region Append && Delete && Find
		public static XmlNode FindNode(string fileName, string mainNode , string node , string val)
		{
			XmlNodeList xmlNodes = GetNodes(fileName,mainNode);
			if(xmlNodes != null)
			{
				foreach(XmlNode xmlNode in xmlNodes)
				{
					if(xmlNode.NodeType == XmlNodeType.Element && xmlNode.InnerText == val)
					{
						return xmlNode;
					}
				}
			}
			return null;
		}
		public static XmlNode AppendNode(string fileName, string mainNode , string node , string val)
		{
			if(FindNode(fileName,mainNode,node,val)==null)
			{
				return SetNewNode(fileName,mainNode,node,val);
			}
			else
			{
				return null;
			}
		}
		public static void DeleteNode(string fileName, string mainNode , string node , string val)
		{
			if(fileName.Trim().Length==0 || mainNode.Trim().Length==0 || node.Trim().Length==0)
			{
				return ;
			}
			if(!Char.IsLetter(node[0])&&node[0]!='_')
			{
				node = '_'+node;
			}
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				XmlNodeList xmlNodes = doc[mainNode].ChildNodes;
				XmlNode _xmlnode = null;
				if(xmlNodes != null)
				{
					foreach(XmlNode xmlNode in xmlNodes)
					{
						if(xmlNode.NodeType == XmlNodeType.Element && xmlNode.InnerText == val)
						{
							_xmlnode = xmlNode;
							break;
						}
					}
				}
				if(_xmlnode != null)
				{
					doc[mainNode].RemoveChild(_xmlnode);
				}
				doc.Save(fileName);
			}
			catch
			{
			}
		}
		#endregion
		#endregion

		#region Simple Override Version
		public static void SetNode(string node , string val)
		{
			SetNode(defaultFileName,defaultMainNode,node,val);
		}
		public static XmlNode GetNode(string node)
		{
			return GetNode(defaultFileName,defaultMainNode,node);
		}
		public static string GetNodeValue(string node)
		{
			return GetNodeValue(defaultFileName,defaultMainNode,node);
		}
		public static string GetNoteValueWithDefault(string node,string defaultValue)
		{
			return GetNoteValueWithDefault(defaultFileName,defaultMainNode,node,defaultValue);
		}
		public static void RemoveNode(string node)
		{
			RemoveNode(defaultFileName,defaultMainNode,node);
		}
		public static void RemoveAllNodes()
		{
			RemoveAllNodes(defaultFileName,defaultMainNode);
		}
		public static string GetContent()
		{
			return GetContent(defaultFileName,defaultMainNode);
		}
		public static XmlNodeList GetNodes()
		{
			return GetNodes(defaultFileName,defaultMainNode);
		}

		#endregion

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -