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

📄 xmlconsole.cs

📁 小型搜索软件的源代码
💻 CS
字号:
using System;
using System.Xml;
using System.Collections;

namespace ShootSearch.Helper
{
	/// <summary>
	/// XMLConsole 的摘要说明。
	/// </summary>
	public class XMLConsole
	{
		private XmlDocument mDoc;

		#region General Loading, Saving, and Initialization

		public string DocumentXml
		{
			get
			{
				return mDoc.InnerXml;
			}
		}

		/// <summary>
		/// XmlDocument Property
		/// </summary>
		public XmlDocument Document
		{
			get
			{
				return mDoc;
			}
			set
			{
				mDoc = value;
			}
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public XMLConsole()
		{
			mDoc = new XmlDocument();
		}

		/// <summary>
		/// Loads an XML File
		/// </summary>
		/// <param name="filename">The file to load</param>
		public void loadFile(string filename)
		{
			mDoc.Load(filename);
		}

		/// <summary>
		/// Loads a string of XML
		/// </summary>
		/// <param name="xml">The XML to load</param>
		public void loadString(string xml)
		{
			mDoc.LoadXml(xml);
		}

		/// <summary>
		/// Saves the XML document
		/// </summary>
		/// <param name="filename">The file name to save as</param>
		public void saveDocument(string filename)
		{
			mDoc.Save(filename);
		}
		#endregion

		#region Attribute Handling

		/// <summary>
		/// Creates (or sets the value of) an attribute
		/// </summary>
		/// <param name="xpath">The XPath to the node containing the attribute</param>
		/// <param name="name">The name of the attribute</param>
		/// <param name="val">The value of the attribute</param>
		public void setAttribute(string xpath, string name, string val)
		{
			XmlAttribute attr = mDoc.CreateAttribute(name);
			
			attr.Value = val;

			mDoc.SelectSingleNode(xpath).Attributes.SetNamedItem(attr);
		}

		/// <summary>
		/// Returns the value of a specified attribute
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <param name="name">The name of the attribute</param>
		/// <returns>The value of an attribute</returns>
		public string getAttribute(string xpath, string name)
		{
			XmlAttribute attr = mDoc.SelectSingleNode(xpath).Attributes[name];
			if(attr == null)
				return "" ;
			else
                return attr.Value;
		}

		/// <summary>
		/// Returns the value of a specified attribute
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <param name="name">The name of the attribute</param>
		/// <param name="defValue">The default value of the attribute</param>
		/// <returns>The value of an attribute</returns>
		public string getAttribute(string xpath, string name ,string defValue)
		{
			XmlAttribute attr = mDoc.SelectSingleNode(xpath).Attributes[name];
			if(attr == null)
				return defValue ;
			else
				return attr.Value;
		}

		#endregion

		#region Node Handling

		/// <summary>
		/// Sets the text of a node
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <param name="text">The new text for the node</param>
		public void setNodeText(string xpath, string text)
		{
			XmlNode node = mDoc.SelectSingleNode(xpath);

			node.InnerText = text;
		}

		/// <summary>
		/// Returns the text of a node
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <returns>The text in the node</returns>
		public string getNodeText(string xpath)
		{
			XmlNode node = mDoc.SelectSingleNode(xpath);

			return node.InnerText;
		}

		/// <summary>
		/// Sets the XML of a node
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <param name="xml">The nodes new XML</param>
		public void setNodeXml(string xpath, string xml)
		{
			XmlNode node = mDoc.SelectSingleNode(xpath);

			node.InnerXml = xml;
		}

		/// <summary>
		/// Returns the XML for a node
		/// </summary>
		/// <param name="xpath">The path to the node</param>
		/// <returns>The nodes current XML</returns>
		public string getNodeXml(string xpath)
		{
			XmlNode node = mDoc.SelectSingleNode(xpath);
			
			return node.InnerXml;
		}

		/// <summary>
		/// 获取节点列表
		/// </summary>
		/// <param name="xpath"></param>
		/// <returns></returns>
		public ArrayList getNodeTextList(string xpath)
		{
			return  getNodeTextList(xpath,0) ;		
		}


		/// <summary>
		/// 获取节点列表
		/// </summary>
		/// <param name="xpath"></param>
		/// <param name="FormatType">0 不格式化 1 大写格式化 2 小写格式化</param>
		/// <returns></returns>
		public ArrayList getNodeTextList(string xpath , int FormatType)
		{

			XmlNode node = mDoc.SelectSingleNode(xpath);
			ArrayList arrayNode = new ArrayList() ;
			System.Xml.XmlNodeReader xmlreader = new XmlNodeReader(node);
			while(xmlreader.Read())
			{
				if(xmlreader.Value != "" )
				{
					if(FormatType == 0)
					{
						arrayNode.Add(xmlreader.Value);
					}
					else if (FormatType == 1)
					{
						arrayNode.Add(xmlreader.Value.ToUpper());
					}
					else
					{
						arrayNode.Add(xmlreader.Value.ToLower());
					}
				
				}
			}
			return  arrayNode;
			
		}

		/// <summary>
		/// 存储节点列表
		/// </summary>
		/// <param name="xpath">路径</param>
		/// <param name="xnode">节点名</param>
		/// <param name="xlist">节点值列表</param>
		public void setNodeTextList(string xpath,string xnode,ArrayList xlist)
		{

			XmlNode node = mDoc.SelectSingleNode(xpath);
			string xml = "";
			for(int i = 0 ; i< xlist.Count ; i++)
			{
				xml += "<" + xnode + ">" + System.Web.HttpUtility.HtmlEncode(xlist[i].ToString()) + "</" + xnode + ">" ;
			}
			node.InnerXml = xml ;
			
		}

		#endregion
	}
}

⌨️ 快捷键说明

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