标签:appdata ini company 文件 pat child imu www. unit
该类包含了对XML文件的创建,添加,读取,删除,修改等操作
//#define isUnity #if isUnity using UnityEngine; #endif using System.Collections; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Linq; public class XMLoperate { #region 创建 ///实例路径fliepath: C:/Users/Administrator/AppData/LocalLow/DefaultCompany/Racingsimulator/test.xml ///实例路径elePath:”/ABC/AAA”, /// <summary> /// 创建XML文件 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”RootEle”>根元素</param> /// <param name=”eles”>一级元素</param> public static void CreateXML(string fliepath,string RootEle,string [] eles) { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration(“1.0”, “utf-8”, null); doc.AppendChild(dec); //创建一个根节点(一级) XmlElement root = doc.CreateElement(RootEle); doc.AppendChild(root); //创建节点(二级) for (int i = 0; i < eles.Length; i++) { XmlNode Nodel = doc.CreateElement(eles[i]); Nodel.InnerText = “”; root.AppendChild(Nodel); } doc.Save(fliepath); } /// <summary> /// 创建XML文件 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”RootEle”>根元素</param> /// <param name=”eles”>一级元素</param> /// <param name=”elesvalue”>一级元素的值</param> public static void CreateXML(string fliepath, string RootEle, string[] eles,string [] elesvalue) { if (eles.Length == elesvalue.Length) { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration(“1.0”, “utf-8”, null); doc.AppendChild(dec); //创建一个根节点(一级) XmlElement root = doc.CreateElement(RootEle); doc.AppendChild(root); //创建节点(二级) for (int i = 0; i < eles.Length; i++) { XmlNode Nodel = doc.CreateElement(eles[i]); Nodel.InnerText = elesvalue[i]; root.AppendChild(Nodel); } doc.Save(fliepath); } else { throw new Exception(“eles和elesvalue的长度不一致”); } } #endregion #region 添加 /// <summary> /// 向XML指定元素内添加一组元素 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径param> /// <param name=”eles”>元素</param> public static void AddXmlElementsToOneElement(string fliepath,string elePath, string[] eles) { XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); for (int i = 0; i < list.Count; i++) { for (int j = 0; j < eles.Length; j++) { XmlNode Nodel = doc.CreateElement(eles[j]); Nodel.InnerText = “”; list[i].AppendChild(Nodel); } } doc.Save(fliepath); } /// <summary> /// 向XML指定元素内添加一组元素 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径param> /// <param name=”eles”>元素</param> /// <param name=”elesvalue”>元素值</param> public static void AddXmlElementsToOneElement(string fliepath, string elePath, string[] eles, string[] elesvalue) { XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); for (int i = 0; i < list.Count; i++) { for (int j = 0; j < eles.Length; j++) { XmlNode Nodel = doc.CreateElement(eles[j]); Nodel.InnerText = elesvalue[j]; list[i].AppendChild(Nodel); } } doc.Save(fliepath); } #endregion #region 读取 /// <summary> /// 从一个指定元素节点获取指定子元素的值 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径param> /// <param name=”eles”>元素名称</param> /// <returns></returns> public static string [] ReadXmlElmentsFromOneElement(string fliepath, string elePath, string[] eles) { string[] redN = new string[eles.Length]; XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); if (list.Count > 0) { for (int j = 0; j < eles.Length; j++) { redN[j] = list[0].SelectNodes(eles[j])[0].InnerText; } } return redN; } /// <summary> /// 从一个指定的元素节点获取相同路径下指定的节点 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径</param> /// <param name=”elePath2″>相同元素路径</param> /// <param name=”eles”>元素名称</param> /// <returns></returns> public static List <string > ReadALLXmlElmentsFromOneElement(string fliepath, string elePath, string elePath2, string[] eles) { List<string> redN = new List<string>(); XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); if (list.Count > 0) { XmlNodeList list1 = list[0].SelectNodes(elePath2); for (int i = 0; i < list1.Count; i++) { for (int j = 0; j < eles.Length; j++) { string mk = list1[i].SelectNodes(eles[j])[0].InnerText; redN.Add(mk); } } } return redN; } #endregion #region 修改 /// <summary> /// 修改某一路径下的所有eles中指定的元素值为elesvalue /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径</param> /// <param name=”eles”>元素名称</param> /// <param name=”elesvalue”>元素值</param> public static void ModifyXmlelments(string fliepath, string elePath, string[] eles, string[] elesvalue) { if (eles.Length == elesvalue.Length) { XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); for (int i = 0; i < eles.Length; i++) { list[0].SelectNodes(eles[i])[0].InnerText =elesvalue [i]; } doc.Save(fliepath); } else { throw new Exception(“eles和elesvalue的长度不一致”); } } #endregion #region 删除 /// <summary> /// 删除指定路径下的所有指定元素 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径</param> /// <param name=”eles”>元素名称</param> public static void DeleteXmlelemnts(string fliepath, string elePath, string[] eles) { XmlDocument doc = new XmlDocument(); doc.Load(fliepath); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); for (int i = 0; i < eles.Length; i++) { XmlNode mmk = list[0].SelectNodes(eles[i])[0]; list[0].RemoveChild(mmk); } doc.Save(fliepath); } #endregion #if isUnity #region Resource文件夹读取 /// <summary> /// 从一个指定元素节点获取指定子元素的值 /// </summary> /// <param name=”fliepath”>文件路径</param> /// <param name=”elePath”>元素路径param> /// <param name=”eles”>元素名称</param> /// <returns></returns> public static string[] ResourceReadXmlElmentsFromOneElement(string fliepath, string elePath, string[] eles) { string[] redN = new string[eles.Length]; XmlDocument doc = new XmlDocument(); doc.LoadXml(((TextAsset)Resources.Load(fliepath)).text); XmlElement root = doc.DocumentElement; XmlNodeList list = root.SelectNodes(elePath); if (list.Count > 0) { for (int j = 0; j < eles.Length; j++) { redN[j] = list[0].SelectNodes(eles[j])[0].InnerText; } } return redN; } #endregion #endif }
原文:https://www.shiweikeji.club/?p=225
标签:appdata ini company 文件 pat child imu www. unit
原文地址:https://www.cnblogs.com/mathb/p/9900292.html