C#對XML操作類


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”,
/// <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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM