//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.IO; using System.Xml; namespace TouchSocket.Core { /// /// xml主类 /// public class XmlTool { /// /// 构造函数 /// /// 文件路径,包含文件名 public XmlTool(string path) { this.path = path; } private readonly string path = null; #region 存储 /// /// 单节点,单属性储存 /// /// 节点名 /// 属性名 /// 属性值 public void AttributeStorage(string NodeName, string Attribute_name, string Attribute_value) { if (File.Exists(path)) {//存在Xml的文件 XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; bool N = false;//节点判断变量 foreach (XmlNode item in nodeList) {//判断是否存在该节点 if (item.Name == NodeName) { N = true; break; } } if (N == false) {//不存在节点,属性,建立节点,属性 XmlElement PointName = xml.CreateElement(NodeName); PointName.SetAttribute(Attribute_name, Attribute_value); root.AppendChild(PointName); } else {//存在属性进行赋值 XmlNode PointName = xml.SelectSingleNode("Root/" + NodeName); PointName.Attributes[Attribute_name].Value = Attribute_value; } xml.Save(path); } else { XmlDocument xml = new XmlDocument(); XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = xml.CreateElement("Root"); xml.AppendChild(root);//根元素 XmlElement PointName = xml.CreateElement(NodeName); PointName.SetAttribute(Attribute_name, Attribute_value); root.AppendChild(PointName); xml.Save(path); } } /// /// 单节点,多属性存储 /// /// 节点名 /// 属性集合 /// 属性值集合 public void AttributeStorage(string NodeName, string[] Attribute_name, string[] Attribute_value) { if (Attribute_name.Length != Attribute_value.Length) { Console.WriteLine("属性名数量和属性值数量不一致,无法储存"); return; } if (File.Exists(path)) {//存在Xml的文件 XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; bool N = false;//节点变量 foreach (XmlNode item in nodeList) {//判断是否存在该节点 if (item.Name == NodeName) { N = true; break; } } if (N == false) {//不存在节点,属性,建立节点,属性 XmlElement PointName = xml.CreateElement(NodeName); for (int i = 0; i < Attribute_name.Length; i++) { PointName.SetAttribute(Attribute_name[i], Attribute_value[i]); root.AppendChild(PointName); } } else {//存在属性进行赋值 XmlNode PointName = xml.SelectSingleNode("Root/" + NodeName); for (int i = 0; i < Attribute_name.Length; i++) { PointName.Attributes[Attribute_name[i]].Value = Attribute_value[i]; } } xml.Save(path); } else { XmlDocument xml = new XmlDocument(); XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = xml.CreateElement("Root"); xml.AppendChild(root);//根元素 XmlElement PointName = xml.CreateElement(NodeName); for (int i = 0; i < Attribute_name.Length; i++) { PointName.SetAttribute(Attribute_name[i], Attribute_value[i]); root.AppendChild(PointName); } xml.Save(path); } } /// /// 单节点,单属性多集合存储 /// /// 节点集合 /// 属性名集合 /// 属性值集合 public void AttributeStorage(string[] NodeName, string[] Attribute_name, string[] Attribute_value) { if ((Attribute_name.Length != Attribute_value.Length) && NodeName.Length != Attribute_name.Length) { Console.WriteLine("属性名数量和属性值数量不一致,无法储存"); return; } if (File.Exists(path)) {//存在Xml的文件 XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; for (int i = 0; i < NodeName.Length; i++) { bool N = false;//节点变量 foreach (XmlNode item in nodeList) {//判断是否存在该节点 if (item.Name == NodeName[i]) { N = true; break; } } if (N == false) {//不存在节点,属性,建立节点,属性 XmlElement PointName = xml.CreateElement(NodeName[i]); PointName.SetAttribute(Attribute_name[i], Attribute_value[i]); root.AppendChild(PointName); } else {//存在属性进行赋值 XmlNode PointName = xml.SelectSingleNode("Root/" + NodeName); PointName.Attributes[Attribute_name[i]].Value = Attribute_value[i]; } xml.Save(path); } } else { XmlDocument xml = new XmlDocument(); XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = xml.CreateElement("Root"); xml.AppendChild(root);//根元素 for (int i = 0; i < NodeName.Length; i++) { XmlElement PointName = xml.CreateElement(NodeName[i]); PointName.SetAttribute(Attribute_name[i], Attribute_value[i]); root.AppendChild(PointName); xml.Save(path); } } } /// /// 多节点,多属性,多集合存储 /// /// 节点集合 /// 属性集合 /// 每个节点的属性数量 /// 属性值集合 public void AttributeStorage(string[] NodeName, string[] Attribute_name, int AttributeNumber, params string[][] Attribute_value) { if (File.Exists(path)) { //存在Xml的文件 XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; for (int i = 0; i < NodeName.Length; i++) { bool N = false;//节点变量 foreach (XmlNode item in nodeList) {//判断是否存在该节点 if (item.Name == NodeName[i]) { N = true; break; } } if (N == false) {//不存在节点,属性,建立节点,属性 XmlElement PointName = xml.CreateElement(NodeName[i]); for (int j = 0; j < AttributeNumber; j++) { PointName.SetAttribute(Attribute_name[j], Attribute_value[j][i]); } root.AppendChild(PointName); } else {//存在属性进行赋值 XmlNode PointName = xml.SelectSingleNode("Root/" + NodeName[i]); for (int j = 0; j < AttributeNumber; j++) { PointName.Attributes[Attribute_name[j]].Value = Attribute_value[j][i]; } } } xml.Save(path); } else { XmlDocument xml = new XmlDocument(); XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = xml.CreateElement("Root"); xml.AppendChild(root);//根元素 for (int i = 0; i < NodeName.Length; i++) { XmlElement PointName = xml.CreateElement(NodeName[i]); for (int j = 0; j < AttributeNumber; j++) { PointName.SetAttribute(Attribute_name[j], Attribute_value[j][i]); } root.AppendChild(PointName); xml.Save(path); } } } /// /// 节点值存储 /// /// 节点名 /// 文本 public void NodeStorage(string NodeName, string Text) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; bool n = false; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { item.InnerText = Text; n = true; break; } } if (n == false) { XmlElement other = xml.CreateElement(NodeName); other.InnerText = Text; root.AppendChild(other); } xml.Save(path); } else { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement Root = doc.CreateElement("Root"); doc.AppendChild(Root);//根元素 XmlElement Node = doc.CreateElement(NodeName); Node.InnerText = Text; Root.AppendChild(Node); doc.Save(path); } } #endregion 存储 #region /// /// 通过节点取值 /// /// 节点名 /// 取值失败返回null public string SearchNode(string NodeName) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { return item.InnerText; } } } return null; } /// /// 查找数字 /// /// 节点名 /// 属性名 /// 取值失败返回0 public int SearchNumber(string NodeName, string Attribute_name) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { if (item.Attributes[Attribute_name] != null) { return Convert.ToInt32(item.Attributes[Attribute_name].Value); } } } } return 0; } /// /// 查找属性值 /// /// 节点名 /// 属性名 /// 取值失败返回null public string SearchWords(string NodeName, string Attribute_name) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { if (item.Attributes[Attribute_name] != null) { return item.Attributes[Attribute_name].Value; } } } } return null; } /// /// 查找布尔值 /// /// 节点名 /// 属性值 /// 返回查找结果,查询失败返回false public bool SearchBoolean(string NodeName, string Attribute_name) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { if (item.Attributes[Attribute_name] != null) { try { return Convert.ToBoolean(item.Attributes[Attribute_name].Value); } catch { return false; } } } } } return false; } /// /// 查找属性值集合 /// /// 节点名集合 /// 属性名集合 /// 文件不在返回null,单个属性不在返回“空” public string[] SearchWords(string[] NodeName, string[] Attribute_name) { if (File.Exists(path)) { string[] s = new string[NodeName.Length]; XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; for (int i = 0; i < NodeName.Length; i++) { foreach (XmlNode item in nodeList) { if (item.Name == NodeName[i]) { if (item.Attributes[Attribute_name[i]] != null) { s[i] = item.Attributes[Attribute_name[i]].Value; } else { s[i] = ""; } } } } return s; } return null; } /// /// 通过确切属性值,属性名,查找其他属性值 /// /// 已知属性名 /// 已知属性值 /// 待查属性名 /// 待查属性值 public string[] SearchWords(string Attribute_name1, string Attribute_value, string Attribute_name2) { List values = new List(); if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Attributes[Attribute_name1] != null) { if (item.Attributes[Attribute_name1].Value == Attribute_value) { if (item.Attributes[Attribute_name2] != null) { values.Add(item.Attributes[Attribute_name2].Value); } } } } } return values.ToArray(); } /// /// 查找节点的所有属性值 /// /// 节点 名 /// 返回查找键值对,查询失败返回null public Dictionary SearchAllAttributes(string NodeName) { Dictionary Attributes = new Dictionary(); if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { XmlAttributeCollection attributeCollection = item.Attributes; if (attributeCollection != null) { foreach (XmlAttribute attribute in attributeCollection) { Attributes.Add(attribute.Name, attribute.Value); } } return Attributes; } } } return null; } /// /// 通过确切属性值,属性名,查找其他属性的布尔值 /// /// 已知属性名 /// 已知属性值 /// 待查属性名 /// 待查布尔值,失败返回false public bool SearchBoolean(string Attribute_name1, string Attribute_value, string Attribute_name2) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Attributes[Attribute_name1].Value == Attribute_value) { if (item.Attributes[Attribute_name2] != null) { try { return Convert.ToBoolean(item.Attributes[Attribute_name2].Value); } catch { return false; } } } } } return false; } #endregion /// /// 按节点名移除节点 /// /// 节点名 /// 是否移除成功 public bool RemoveNode(string NodeName) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { root.RemoveChild(item); xml.Save(path); return true; } } } return false; } /// /// 按确切的属性名,属性值删除节点 /// /// 属性名 /// 属性值 /// 是否移除成功 public bool RemoveNode(string Attribute_name, string Attribute_value) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Attributes[Attribute_name] != null) { if (item.Attributes[Attribute_name].Value == Attribute_value) { root.RemoveChild(item); xml.Save(path); return true; } } } } return false; } /// /// 如果节点中有日期属性,把日期之前的节点都删除 /// /// 属性名 /// 截止时间 /// 是否删除成功 public bool RemoveNode(string Attribute_name, DateTime dateTime) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; for (int i = 0; i < nodeList.Count; i++) { if (nodeList[i].Attributes[Attribute_name] != null) { DateTime dt = Convert.ToDateTime(nodeList[i].Attributes[Attribute_name].Value); if (DateTime.Compare(dt, dateTime) < 0) { root.RemoveChild(nodeList[i]); } } } xml.Save(path); return true; } return false; } /// /// 判断节点是否存在 /// /// 节点名 /// 返回结果 public bool NodeExist(string NodeName) { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == NodeName) { return true; } } } return false; } /// /// 删除所有节点,不包含子节点 /// /// 返回删除是否成功 public bool RemoveAllNode() { if (File.Exists(path)) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlElement root = xml.DocumentElement; root.RemoveAll(); xml.Save(path); return true; } return false; } } }