using System.IO; using System.Xml; using System.Xml.Linq; namespace Sai.Toolkit.Core.Helpers; public class XmlHelper { public XmlHelper(string strPath) { XmlPath = strPath; } public string XmlPath { get; } /// /// 创建配置文件,可在子类中重写此方法 /// public static void CreateFile(string fileName) { var doc = new XmlDocument(); var dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); var root = doc.CreateElement("Root"); doc.AppendChild(root); root.AppendChild(doc.CreateElement("Config")); doc.Save(fileName); } /// /// 删除节点值 /// /// 路径 /// 节点 public static void Delete(string path, string node) { try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); xn.ParentNode.RemoveChild(xn); doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } /// /// 删除数据 /// /// 路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 public static void Delete(string path, string node, string attribute) { try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); var xe = (XmlElement)xn; if (attribute.Equals(string.Empty)) { xn.ParentNode.RemoveChild(xn); } else { xe.RemoveAttribute(attribute); } doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 属性名,非空时插入该元素属性值,否则插入元素值 /// 值 public static void Insert(string path, string node, string element, string attribute, string value) { try { var doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory + path); var xn = doc.SelectSingleNode(node); if (element.Equals(string.Empty)) { if (!attribute.Equals(string.Empty)) { var xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { var xe = doc.CreateElement(element); if (attribute.Equals(string.Empty)) { xe.InnerText = value; } else { xe.SetAttribute(attribute, value); } xn.AppendChild(xe); } doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 由XML属性名和值组成的二维数组 public static void Insert(string path, string node, string element, string[][] strList) { try { var doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory + path); var xn = doc.SelectSingleNode(node); var xe = doc.CreateElement(element); var strAttribute = string.Empty; var strValue = string.Empty; for (var i = 0; i < strList.Length; i++) { for (var j = 0; j < strList[i].Length; j++) { if (j == 0) { strAttribute = strList[i][j]; } else { strValue = strList[i][j]; } } if (strAttribute.Equals(string.Empty)) { xe.InnerText = strValue; } else { xe.SetAttribute(strAttribute, strValue); } } xn.AppendChild(xe); doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } public static void parsexmldoc(string file) { var document = XDocument.Load(file); //获取到XML的根元素进行操作 var root = document.Root; if (root.FirstAttribute.Value == "车辆轮廓点") { //获取根元素下的所有子元素 var enumerable = root.Elements(); foreach (var xe in enumerable) { //IEnumerable pts = xe.Elements(); //KineModel data = new KineModel(); //data.StrPosition = xe.Name.LocalName; //data.Name = (string)xe.Attribute("点号"); //data.X = (double)xe.Attribute("X"); //data.Y = (double)xe.Attribute("Y"); //vm.Items.Add(data); } } } /// /// 读取指定节点的数据 /// /// 节点 /// The public string Read(string node) { var value = string.Empty; try { var doc = XmlLoad(); var xn = doc.SelectSingleNode(node); if (xn != null) { value = xn.InnerText; } } catch { // ignored } return value; } /// /// 读取指定路径和节点的串联值 /// /// 路径 /// 节点 /// The public static string Read(string path, string node) { var value = string.Empty; try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { // ignored } return value; } /// /// 读取指定路径和节点的属性值 /// /// 路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 /// The public static string Read(string path, string node, string attribute) { var value = string.Empty; try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); value = attribute.Equals(string.Empty) ? xn.InnerText : xn.Attributes[attribute].Value; } catch { // ignored } return value; } /// /// 获取某一节点的所有子节点的值 /// /// 要查询的节点 /// The public XmlNodeList ReadAllChild(string node) { var doc = XmlLoad(); var xn = doc.SelectSingleNode(node); var nodelist = xn.ChildNodes; //得到该节点的子节点 return nodelist; } /// /// 获取某一节点的所有孩子节点的值 /// /// 要查询的节点 public string[] ReadAllChildallValue(string node) { var i = 0; string[] str = { }; var doc = XmlLoad(); var xn = doc.SelectSingleNode(node); if (xn != null) { var nodelist = xn.ChildNodes; //得到该节点的子节点 if (nodelist.Count > 0) { str = new string[nodelist.Count]; foreach (XmlElement el in nodelist) //读元素值 { str[i] = el.Value; i++; } } } return str; } /// /// 修改指定节点的数据 /// /// 节点 /// 值 public void Update(string node, string value) { try { var doc = XmlLoad(); var xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory + XmlPath); } catch { // ignored } } /// /// 修改指定节点的数据 /// /// 路径 /// 节点 /// 值 public static void Update(string path, string node, string value) { try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } /// /// 修改指定节点的属性值(静态) /// /// 路径 /// 节点 /// 属性名,非空时修改该节点属性值,否则修改节点值 /// 值 public static void Update(string path, string node, string attribute, string value) { try { var doc = XmlLoad(path); var xn = doc.SelectSingleNode(node); var xe = (XmlElement)xn; if (attribute.Equals(string.Empty)) { xe.InnerText = value; } else { xe.SetAttribute(attribute, value); } doc.Save(AppDomain.CurrentDomain.BaseDirectory + path); } catch { // ignored } } private XmlDocument XmlLoad() { var XMLFile = XmlPath; var xmldoc = new XmlDocument(); try { var filename = AppDomain.CurrentDomain.BaseDirectory + XMLFile; if (File.Exists(filename)) { xmldoc.Load(filename); } } catch (Exception) { // ignored } return xmldoc; } /// /// 导入XML文件 /// /// The strPath /// The private static XmlDocument XmlLoad(string strPath) { var xmldoc = new XmlDocument(); try { var filename = AppDomain.CurrentDomain.BaseDirectory + strPath; if (File.Exists(filename)) { xmldoc.Load(filename); } } catch (Exception) { // ignored } return xmldoc; } }