using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.IO;public class XmlDoc { XmlDocument doc; string xmlFileName; readonly string xmlRootName; public XmlNode xmlRoot; //设置XML文件名 public string FileName { set { xmlFileName = HttpContext.Current.Server.MapPath(value); } } ////// 类初始化 /// /// 文件夹 /// xml文件名 /// xml根节点 public XmlDoc(string path, string filename, string root) { xmlRootName = root; xmlFileName = HttpContext.Current.Server.MapPath(path) + "\\" + filename; doc = new XmlDocument(); try { doc.Load(xmlFileName); } catch { doc.LoadXml(" <" + root + "> "); } xmlRoot = doc.SelectSingleNode(xmlRootName); } ////// 类初始化(从内容实例化XML) /// /// 内容 /// xml根节点 public XmlDoc(string Content, string root) { xmlRootName = root; doc = new XmlDocument(); try { doc.LoadXml(Content); } catch { doc.LoadXml(" <" + xmlRootName + "> "); } xmlRoot = doc.SelectSingleNode(xmlRootName); xmlFileName = HttpContext.Current.Server.MapPath(root + ".xml"); } ////// 保存XML文件 /// ///public void Save() { try { string server_path = Helper.GetServerPath(xmlFileName); if (!Directory.Exists(server_path)) { Directory.CreateDirectory(server_path); } doc.Save(xmlFileName); } catch { return; } } /// /// 删除XML文件 /// ///public void Delete() { try { if (File.Exists(xmlFileName)) { File.Delete(xmlFileName); } } catch { return; } } /// /// 获取类里的XmlDocument /// ///public XmlDocument Doc { get { return doc; } } /// /// 对类里的XmlDocument重新初始化 /// ///public void DocInit() { doc = new XmlDocument(); doc.LoadXml(" <" + xmlRoot + "> "); } /// /// 获取类里的XmlDocument一个特定XmlElement /// /// 父节点 /// 节点名称 /// 节点数组下标值 /// 是否自动创建 ///XmlElement public XmlElement GetXmlNodeByTagNameAndIndex(XmlElement parentNode, string tagName, int index, bool IsAutoCreate) { XmlElement xe; if (index < 0) { return null; } try { XmlNodeList elemList = parentNode.SelectNodes(tagName); //自动创建 if (elemList.Count <= index && IsAutoCreate) { for (int i = elemList.Count; i <= index; i++) { parentNode.AppendChild(doc.CreateElement(tagName)); } elemList = parentNode.SelectNodes(tagName); } XmlNode xn = elemList[index]; xe = (XmlElement)xn; } catch { xe = null; } return xe; } ////// 获取类里的XmlDocument一个特定XmlElement(根据节点属性值查找) /// /// 父节点 /// 节点名称 /// 节点属性名称 /// 节点属性值 /// 是否自动创建 ///XmlElement public XmlElement GetXmlNodeByTagNameAndAttribute(XmlElement parentNode, string tagName, string AttributeName, string AttributeValue, bool IsAutoCreate) { bool IsExists = false; XmlElement xe = null; string Value = ""; try { XmlNodeList elemList = parentNode.SelectNodes(tagName); if (elemList != null) { foreach (XmlNode xn in elemList) { if (xn.NodeType == XmlNodeType.Element) { XmlElement xel = (XmlElement)xn; if (xel.HasAttribute(AttributeName)) { Value = xel.GetAttribute(AttributeName); } if (Value == AttributeValue) { IsExists = true; xe = xel; break; } } } } //自动创建 if (!IsExists && IsAutoCreate) { XmlElement elem = doc.CreateElement(tagName); elem.SetAttribute(AttributeName, AttributeValue); parentNode.AppendChild(elem); xe = elem; } } catch { xe = null; } return xe; } ////// 获取类里的XmlDocument一个特定XmlElement(根节点作父节点) /// /// 节点名称 /// 节点数组下标值 /// 是否自动创建 ///XmlElement public XmlElement GetXmlNodeByTagNameAndIndex(string tagName, int index, bool IsAutoCreate) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetXmlNodeByTagNameAndIndex(ParentNode, tagName, index, IsAutoCreate); } ////// 获取类里的XmlDocument一个特定XmlElement(根节点作父节点,根据节点属性值查找) /// /// 节点名称 /// 节点属性名称 /// 节点属性值 /// 是否自动创建 ///XmlElement public XmlElement GetXmlNodeByTagNameAndAttribute(string tagName, string AttributeName, string AttributeValue, bool IsAutoCreate) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetXmlNodeByTagNameAndAttribute(ParentNode, tagName, AttributeName, AttributeValue, IsAutoCreate); } ////// 为类里的XmlDocument一个特定XmlElement设置子CData节点 /// /// 节点 /// 子节点名称 /// 子节点内容 ///public void SetXmlCDataNode(XmlElement xe, string tagName, string tagValue) { XmlNode elemList = xe.SelectSingleNode(tagName); if (elemList != null) { if (elemList.FirstChild == null) { XmlNode elem = elemList.AppendChild(doc.CreateCDataSection(tagValue)); elemList.AppendChild(elem); } else { elemList.FirstChild.Value = tagValue; } } else { XmlElement elem = doc.CreateElement(tagName); XmlNode xn = elem.AppendChild(doc.CreateCDataSection(tagValue)); elem.AppendChild(xn); xe.AppendChild(elem); } } /// /// 为类里的XmlDocument一个特定XmlElement设置子CData节点(根节点作节点) /// /// 子节点名称 /// 子节点内容 ///public void SetXmlCDataNode(string tagName, string tagValue) { XmlElement ParentNode = (XmlElement)xmlRoot; SetXmlCDataNode(ParentNode, tagName, tagValue); } /// /// 获取类里的XmlDocument一个特定XmlElement的子CData节点内容 /// /// 节点 /// 子节点名称 ///string public string GetXmlCData(XmlElement xe, string tagName) { string tagValue = ""; XmlNode elemList = xe.SelectSingleNode(tagName); if (elemList != null) { if (elemList.FirstChild != null) { tagValue = elemList.FirstChild.Value; } } return tagValue; } ////// 获取类里的XmlDocument一个特定XmlElement的子CData节点内容(根节点作节点) /// /// 子节点名称 ///string public string GetXmlCData(string tagName) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetXmlCData(ParentNode, tagName); } ////// 为类里的XmlDocument一个特定XmlElement设置属性 /// /// 节点 /// 节点属性名称 /// 节点属性值 ///public void SetXmlAttribute(XmlElement xe, string tagName, string tagValue) { if (xe.HasAttribute(tagName)) { xe.SetAttribute(tagName, tagValue); } else { xe.SetAttribute(tagName, tagValue); } } /// /// 获取类里的XmlDocument一个特定XmlElement的属性值 /// /// 节点 /// 节点属性名称 ///string public string GetXmlAttribute(XmlElement xe, string tagName) { string tagValue = ""; if (xe.HasAttribute(tagName)) { tagValue = xe.GetAttribute(tagName); } return tagValue; } ////// 移除类里的XmlDocument一个特定XmlElement的属性值 /// /// 节点 /// 节点属性名称 ///public void RemoveXmlAttribute(XmlElement xe, string tagName) { if (xe.HasAttribute(tagName)) { xe.RemoveAttribute(tagName); } } /// /// 移除类里的XmlDocument一个特定XmlElement的子节点 /// /// 节点 /// 子节点名称 ///public void RemoveXmlChildNode(XmlElement xe, string tagName) { XmlNode removeNode = xe.SelectSingleNode(tagName); if (removeNode != null) { xe.RemoveChild(removeNode); } } /// /// 移除类里的XmlDocument一个特定XmlElement(根节点不能移除) /// /// 节点 ///public void RemoveXmlNode(XmlElement xe) { if (xe.ParentNode != null) { xe.ParentNode.RemoveChild(xe); } } /// /// 上移类里的XmlDocument一个特定XmlElement /// /// 父节点 /// 节点名称 /// 节点数组下标值 ///public void Up(XmlElement parentNode, string tagName, int index) { XmlNodeList elemList = parentNode.SelectNodes(tagName); if (elemList != null) { if ((elemList.Count) > index && (index > 0)) { XmlNode refChild = elemList[index]; XmlNode xn = elemList[index - 1]; RemoveXmlNode((XmlElement)xn); parentNode.InsertAfter(xn, refChild); } } } /// /// 上移类里的XmlDocument一个特定XmlElement(根节点作父节点) /// /// 节点名称 /// 节点数组下标值 ///public void Up(string tagName, int index) { XmlElement ParentNode = (XmlElement)xmlRoot; Up(ParentNode, tagName, index); } /// /// 下移类里的XmlDocument一个特定XmlElement /// /// 父节点 /// 节点名称 /// 节点数组下标值 ///public void Down(XmlElement parentNode, string tagName, int index) { XmlNodeList elemList = parentNode.SelectNodes(tagName); if (elemList != null) { if ((elemList.Count - 1) > index && (index >= 0)) { XmlNode refChild = elemList[index]; XmlNode xn = elemList[index + 1]; RemoveXmlNode((XmlElement)xn); parentNode.InsertBefore(xn, refChild); } } } /// /// 下移类里的XmlDocument一个特定XmlElement(根节点作父节点) /// /// 节点名称 /// 节点数组下标值 ///public void Down(string tagName, int index) { XmlElement ParentNode = (XmlElement)xmlRoot; Down(ParentNode, tagName, index); } /// /// 获取类里的XmlDocument一个特定XmlElement下的所有相同节点名的节点数量 /// /// 父节点 /// 节点名称 ///int public int GetNodeCount(XmlElement parentNode, string tagName) { int NodeCount = 0; XmlNodeList elemList = parentNode.SelectNodes(tagName); if (elemList != null) { NodeCount = elemList.Count; } return NodeCount; } ////// 获取类里的XmlDocument一个特定XmlElement下的所有相同节点名的节点数量(根节点作父节点) /// /// 节点名称 ///int public int GetNodeCount(string tagName) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetNodeCount(ParentNode, tagName); } ////// 为类里的XmlDocument一个特定XmlElement设置子Text节点 /// /// 节点 /// 子节点名称 /// 子节点内容 ///public void SetXmlTextNode(XmlElement xe, string tagName, string tagValue) { XmlNode elemList = xe.SelectSingleNode(tagName); if (elemList != null) { if (elemList.FirstChild == null) { XmlNode elem = elemList.AppendChild(doc.CreateTextNode(tagValue)); elemList.AppendChild(elem); } else { elemList.FirstChild.Value = tagValue; } } else { XmlElement elem = doc.CreateElement(tagName); XmlNode xn = elem.AppendChild(doc.CreateTextNode(tagValue)); elem.AppendChild(xn); xe.AppendChild(elem); } } /// /// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容 /// /// 节点 /// 子节点名称 ///string public string GetXmlTextNode(XmlElement xe, string tagName) { string tagValue = ""; XmlNode elemList = xe.SelectSingleNode(tagName); if (elemList != null) { if (elemList.FirstChild != null) { tagValue = elemList.FirstChild.Value; } } return tagValue; } ////// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容 /// /// 节点 /// 子节点名称 /// 节点数组下标值 ///string public string GetXmlText(XmlElement xe, string tagName, int index) { string tagValue = ""; XmlNodeList elemList = xe.SelectNodes(tagName); if (elemList != null) { if ((elemList.Count) > index) { XmlNode refChild = elemList[index]; if (refChild.FirstChild != null) { tagValue = refChild.FirstChild.Value; } } } return tagValue; } ////// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容(根节点作节点) /// /// 子节点名称 /// 节点数组下标值 ///string public string GetXmlText(string tagName, int index) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetXmlText(ParentNode, tagName, index); } ////// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容(根节点作节点) /// /// 子节点名称 ///string public string GetXmlTextNode(string tagName) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetXmlTextNode(ParentNode, tagName); } ////// 获取类里的XmlDocument一个特定XmlElement的所有有特定属性的节点的属性的最大值(属性值必须是INT型) /// /// 父节点 /// 节点名称 /// 节点属性名称 ///int public int GetMaxValueByTagNameAndAttribute(XmlElement parentNode, string tagName, string AttributeName) { int MaxTagValue = 1; XmlNodeList elemList = parentNode.SelectNodes(tagName); if (elemList != null) { foreach (XmlNode xn in elemList) { if (xn.NodeType == XmlNodeType.Element) { XmlElement xel = (XmlElement)xn; if (xel.HasAttribute(AttributeName)) { int iValue = FormChecker.IsNumeric(xel.GetAttribute(AttributeName), 0); if (iValue >= MaxTagValue) { MaxTagValue = iValue + 1; } } } } } return MaxTagValue; } ////// 获取类里的XmlDocument一个特定XmlElement的所有有特定属性的节点的属性的最大值(属性值必须是INT型,根节点作节点) /// /// 节点名称 /// 节点属性名称 ///int public int GetMaxValueByTagNameAndAttribute(string tagName, string AttributeName) { XmlElement ParentNode = (XmlElement)xmlRoot; return GetMaxValueByTagNameAndAttribute(ParentNode, tagName, AttributeName); } ////// 判断类里的XmlDocument一个特定XmlElement是否存在特定节点 /// /// 节点 /// 节点名称 ///int public bool HasAttribute(XmlElement xe, string tagName) { return xe.HasAttribute(tagName); } ////// 获取特定XmlElement的节点值 /// /// 节点 /// 节点名称 ///string public string GetXmlCDataNode(XmlElement xe, string tagName) { string tagValue = ""; XmlNode elemList = xe.SelectSingleNode(tagName); if (elemList != null) { if (elemList.FirstChild != null) { tagValue = elemList.FirstChild.Value; } } return tagValue; } ////// 获取根节点下特定XmlElement的节点值 /// /// 节点名 /// 节点属性名称 /// 节点属性值 ///string public string GetXmlTextNodeByAttribute(string itemName, string tagName, string tagValue) { string value = ""; XmlElement xe = GetXmlNodeByTagNameAndAttribute(itemName, tagName, tagValue, false); if (xe != null) { value = GetXmlCDataNode(xe, "value"); } if (value == "") { value = tagValue; } return value; } }