C#对XML操作类
该类包含了对XML文件的创建,添加,读取,删除,修改等操作
//#define isUnity#if isUnityusing UnityEngine;#endifusing 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”,////// 创建XML文件/// /// 文件路径/// 根元素/// 一级元素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);}////// 创建XML文件/// /// 文件路径/// 根元素/// 一级元素/// 一级元素的值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 添加////// 向XML指定元素内添加一组元素/// /// 文件路径/// 元素路径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);}////// 向XML指定元素内添加一组元素/// /// 文件路径/// 元素路径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 读取////// 从一个指定元素节点获取指定子元素的值/// /// 文件路径/// 元素路径param>/// 元素名称///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;}/// /// 从一个指定的元素节点获取相同路径下指定的节点/// /// 文件路径/// 元素路径/// 相同元素路径/// 元素名称///public static List ReadALLXmlElmentsFromOneElement(string fliepath, string elePath, string elePath2, string[] eles){List redN = new List ();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 修改/// /// 修改某一路径下的所有eles中指定的元素值为elesvalue/// /// 文件路径/// 元素路径/// 元素名称/// 元素值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 删除////// 删除指定路径下的所有指定元素/// /// 文件路径/// 元素路径/// 元素名称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文件夹读取////// 从一个指定元素节点获取指定子元素的值/// /// 文件路径/// 元素路径param>/// 元素名称///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}
原文: