C#对XML操作类
标签:appdata ini company 文件 pat child imu www. unit
C#对XML操作类
该类包含了对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”,
///
/// 创建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 )
{
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 )
{
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 ) {
for (int j = 0; 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 )
{
for (int j = 0; 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 )
{
redN[j] = list[0].SelectNodes(eles[j])[0].InnerText;
}
}
return redN;
}
///
/// 从一个指定的元素节点获取相同路径下指定的节点
///
/// 文件路径
/// 元素路径
/// 相同元素路径
/// 元素名称
///
public static List string > ReadALLXmlElmentsFromOneElement(string fliepath, string elePath, string elePath2, string[] eles)
{
Liststring> redN = new Liststring>();
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 ) {
for (int j = 0; 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 ) {
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 ) {
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 )
{
redN[j] = list[0].SelectNodes(eles[j])[0].InnerText;
}
}
return redN;
}
#endregion
#endif
}
原文:https://www.shiweikeji.club/?p=225
C#对XML操作类
标签:appdata ini company 文件 pat child imu www. unit
原文地址:https://www.cnblogs.com/mathb/p/9900292.html
评论