using System; using System.Collections.Generic; using System.IO; using System.Xml; namespace AutoUpdateTool { public class XmlOperator { private XmlDocument _document = new XmlDocument(); private string _xmlPath; private string _nodePath; public string XmlPath { get { return this._xmlPath; } set { this._xmlPath = value; } } public string NodePath { get { return this._nodePath; } set { this._nodePath = value; } } public XmlOperator(string xmlPath, string nodePath) { this._xmlPath = xmlPath; this._nodePath = nodePath; this._document.Load(xmlPath); } public XmlOperator(string xmlPath) { this._xmlPath = xmlPath; this._document.Load(xmlPath); } public string Out() { return this._document.OuterXml; } public List GetNode(string nodeName) { List result; try { List list = new List(); XmlNodeList elementsByTagName = this._document.GetElementsByTagName(nodeName); foreach (XmlNode xmlNode in elementsByTagName) { list.Add(xmlNode.InnerText); } result = list; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public List GetNode(string path, string nodeName) { List result; try { List list = new List(); XmlNodeList xmlNodeList = this._document.SelectNodes(path); foreach (XmlNode xmlNode in xmlNodeList) { list.Add(xmlNode[nodeName].InnerText); } result = list; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public List> GetNodePair(string path, string nodeName, string nodeName2) { List> result; try { List> list = new List>(); XmlNodeList xmlNodeList = this._document.SelectNodes(path); if (xmlNodeList != null) { foreach (XmlNode xmlNode in xmlNodeList) { XmlElement xmlElement = xmlNode[nodeName]; if (xmlElement != null) { XmlElement xmlElement2 = xmlNode[nodeName2]; if (xmlElement2 != null) { list.Add(new KeyValuePair(xmlElement.InnerText, xmlElement2.InnerText)); } } } } result = list; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public string GetNode(int i, int j, string path) { string innerText; try { XmlNodeList xmlNodeList = this._document.SelectNodes(path); innerText = xmlNodeList[i].ChildNodes[j].InnerText; } catch (Exception ex) { throw new Exception(ex.Message); } return innerText; } public string GetNode(int i, int j) { string innerText; try { XmlNodeList xmlNodeList = this._document.SelectNodes(this._nodePath); innerText = xmlNodeList[i].ChildNodes[j].InnerText; } catch (Exception ex) { throw new Exception(ex.Message); } return innerText; } public string GetNode(int i, string nodePath, string nodeName) { string result; try { XmlNodeList xmlNodeList = this._document.SelectNodes(nodePath); for (int j = 0; j <= xmlNodeList[i].ChildNodes.Count; j++) { if (xmlNodeList[i].ChildNodes[j].Name.Equals(nodeName)) { result = xmlNodeList[i].ChildNodes[j].InnerText; return result; } } result = "nofind"; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public string GetNode(string uncleName, string uncleValue, string nodePath, string parentName, string nodeName) { string result; try { XmlNodeList xmlNodeList = this._document.SelectNodes(nodePath); for (int i = 0; i < xmlNodeList.Count; i++) { XmlElement xmlElement = (XmlElement)xmlNodeList[i]; if (xmlElement.GetElementsByTagName(uncleName)[0].InnerText.Equals(uncleValue)) { result = (xmlElement.GetElementsByTagName(parentName)[0] as XmlElement).GetElementsByTagName(nodeName)[0].InnerText; return result; } } result = ""; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public string GetNode(string siblingName, string siblingValue, string nodePath, string nodeName) { string result; try { XmlNodeList xmlNodeList = this._document.SelectNodes(nodePath); for (int i = 0; i < xmlNodeList.Count; i++) { XmlElement xmlElement = (XmlElement)xmlNodeList[i]; string innerText = xmlElement.GetElementsByTagName(siblingName)[0].InnerText; if (innerText.Equals(siblingValue)) { result = xmlElement.GetElementsByTagName(nodeName)[0].InnerText; return result; } } result = ""; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public string GetNode(int i, string nodeName) { string result; try { XmlNodeList xmlNodeList = this._document.SelectNodes(this._nodePath); for (int j = 0; j <= xmlNodeList[i].ChildNodes.Count; j++) { if (xmlNodeList[i].ChildNodes[j].Name.Equals(nodeName)) { result = xmlNodeList[i].ChildNodes[j].InnerText; return result; } } result = "nofind"; } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public int Count(string nodeName) { int count; try { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(nodeName); count = elementsByTagName.Count; } catch (Exception ex) { throw new Exception(ex.Message); } return count; } public int Count() { int count; try { XmlNodeList xmlNodeList = this._document.SelectNodes(this._nodePath); count = xmlNodeList.Count; } catch (Exception ex) { throw new Exception(ex.Message); } return count; } public int CountChilds(int i, string nodeName) { int result; try { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(nodeName); if (elementsByTagName.Count > 0) { result = elementsByTagName[i].ChildNodes.Count; } else { result = 0; } } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public int CountChilds(string nodeName) { int result; try { int num = 0; XmlNodeList elementsByTagName = this._document.GetElementsByTagName(nodeName); if (elementsByTagName.Count > 0) { for (int i = 0; i < elementsByTagName.Count; i++) { num += elementsByTagName[i].ChildNodes.Count; } result = num; } else { result = 0; } } catch (Exception ex) { throw new Exception(ex.Message); } return result; } public int SetNode(string nodeName, string newValue) { int count; try { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(nodeName); foreach (XmlNode xmlNode in elementsByTagName) { xmlNode.InnerText = newValue; } this._document.Save(this._xmlPath); count = elementsByTagName.Count; } catch (Exception ex) { throw new Exception(ex.Message); } return count; } public void SetNode(int i, string nodePath, string nodeName, string newValue) { try { XmlNodeList xmlNodeList = this._document.SelectNodes(nodePath); if (xmlNodeList.Count <= 0 || xmlNodeList.Count <= i) { throw new Exception("并无此子集"); } for (int j = 0; j < xmlNodeList.Item(i).ChildNodes.Count; j++) { if (xmlNodeList[i].ChildNodes[j].Name == nodeName) { xmlNodeList[i].ChildNodes[j].InnerText = newValue; } } this._document.Save(this._xmlPath); } catch (Exception ex) { throw new Exception(ex.Message); } } public void SetNode(int i, string nodeName, string newValue) { try { XmlNodeList xmlNodeList = this._document.SelectNodes(this._nodePath); if (xmlNodeList.Count > 0 && xmlNodeList.Count > i) { for (int j = 0; j < xmlNodeList.Item(i).ChildNodes.Count; j++) { if (xmlNodeList.Item(i).ChildNodes.Item(j).Name == nodeName) { xmlNodeList.Item(i).ChildNodes.Item(j).InnerText = newValue; } } this._document.Save(this._xmlPath); } } catch (Exception ex) { throw new Exception(ex.Message); } } public void InsetNode(string parentName, string nodeName, string nodeVale) { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(parentName); for (int i = 0; i < elementsByTagName.Count; i++) { XmlElement xmlElement = this._document.CreateElement(nodeName); elementsByTagName.Item(i).AppendChild(xmlElement); xmlElement.InnerText = nodeVale; } this._document.Save(this._xmlPath); } public void InsetNode(int i, string parentName, string nodeName, string nodeValue) { try { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(parentName); XmlElement xmlElement = this._document.CreateElement(nodeName); if (elementsByTagName.Count <= 0) { throw new Exception("无此节点"); } elementsByTagName[i - 1].AppendChild(xmlElement); xmlElement.InnerText = nodeValue; this._document.Save(this._xmlPath); } catch (Exception ex) { throw new Exception(ex.Message); } } public void InsertRootNode(string rootName, string[] nodeName, string[] nodeValue) { XmlElement documentElement = this._document.DocumentElement; XmlElement xmlElement = this._document.CreateElement(rootName); documentElement.AppendChild(xmlElement); for (int i = 0; i < nodeName.Length; i++) { XmlElement xmlElement2 = this._document.CreateElement(nodeName[i]); xmlElement.AppendChild(xmlElement2); xmlElement2.InnerText = nodeValue[i]; } this._document.Save(this._xmlPath); } public void InsertNode(string nodePath, string parentName, string[] nodeName, string[] nodeValue) { XmlNodeList xmlNodeList = this._document.SelectNodes(nodePath); XmlElement xmlElement = this._document.CreateElement(parentName); xmlNodeList[0].AppendChild(xmlElement); for (int i = 0; i < nodeName.Length; i++) { XmlElement xmlElement2 = this._document.CreateElement(nodeName[i]); xmlElement2.InnerText = nodeValue[i]; xmlElement.AppendChild(xmlElement2); } this._document.Save(this._xmlPath); } public void DeleteNode(string parentName, string nodeName) { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(parentName); foreach (XmlNode xmlNode in elementsByTagName) { foreach (XmlNode xmlNode2 in xmlNode.ChildNodes) { if (xmlNode2.Name == nodeName) { xmlNode.RemoveChild(xmlNode2); } } } this._document.Save(this._xmlPath); } public void DeleteNode(string parentName) { XmlNodeList elementsByTagName = this._document.GetElementsByTagName(parentName); foreach (XmlNode xmlNode in elementsByTagName) { xmlNode.RemoveAll(); } this._document.Save(this._xmlPath); } public void DeleteAll() { XmlElement documentElement = this._document.DocumentElement; documentElement.RemoveAll(); this._document.Save(this._xmlPath); } public bool Save(string xmlName, string rootElement) { bool result; try { string filename = Directory.GetCurrentDirectory() + "\\" + xmlName; XmlDocument xmlDocument = new XmlDocument(); XmlElement newChild = xmlDocument.CreateElement(rootElement); xmlDocument.AppendChild(newChild); xmlDocument.Save(filename); result = true; } catch (XmlException ex) { throw new Exception(ex.Message); } return result; } } }