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-”, null);
doc.AppendChild(dec);
//创建一个根节点(一级)
XmlElement root = doc.CreateElement(RootEle);
doc.AppendChild(root);
//创建节点(二级)
for (int i = ; 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-”, null);
doc.AppendChild(dec);
//创建一个根节点(一级)
XmlElement root = doc.CreateElement(RootEle);
doc.AppendChild(root);
//创建节点(二级)
for (int i = ; 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 = ; i < list.Count; i++) { for (int j = ; 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 = ; i < list.Count; i++)
{ for (int j = ; 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 > )
{
for (int j = ; j < eles.Length; j++)
{
redN[j] = list[].SelectNodes(eles[j])[].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 > )
{
XmlNodeList list1 = list[].SelectNodes(elePath2);
for (int i = ; i < list1.Count; i++) { for (int j = ; j < eles.Length; j++) {
string mk = list1[i].SelectNodes(eles[j])[].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 = ; i < eles.Length; i++) { list[].SelectNodes(eles[i])[].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 = ; i < eles.Length; i++) {
XmlNode mmk = list[].SelectNodes(eles[i])[];
list[].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 > )
{
for (int j = ; j < eles.Length; j++)
{
redN[j] = list[].SelectNodes(eles[j])[].InnerText;
} }
return redN;
}
#endregion
#endif
}

原文:https://www.shiweikeji.club/?p=225

C#对XML操作类的更多相关文章

  1. 简单的XML操作类

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  2. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  3. XML Helper XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...

  4. PHP XML操作类DOMDocument

    不得不自已写一个.XML 的操作一直没有用过.下面是自己搜集的XML操作类 DOMDocument相关的内容. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点 ...

  5. C#:XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System; using System.Data; using System.Configuration; using System. ...

  6. c# xml操作类 比较齐全

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  7. c#XML操作类的方法总结

    using System.Xml;using System.Data; namespace DotNet.Utilities{    /// <summary>    /// Xml的操作 ...

  8. XML操作类

        using System; using System.Data; using System.IO; using System.Xml; namespace DotNet.Utilities { ...

  9. c# xml操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xm ...

随机推荐

  1. FairyGUI TextField

    记录一个在使用FairyGUI的TextField时遇到的坑. TextField有一个文本模板功能,可以实现类似占位符的功能,如:{ number = 0 },然后我们可以在脚本中修改number的 ...

  2. ELK学习笔记之ELK搜集OpenStack节点日志

    模板来自网络,模板请不要直接复制,先放到notepad++内调整好格式,注意缩进 部署架构 控制节点作为日志服务器,存储所有 OpenStack 及其相关日志.Logstash 部署于所有节点,收集本 ...

  3. 重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’

    view中导入:from django.contrib.auth.models import AbstractBaseUser settings.py中设置了:AUTH_USER_MODEL='app ...

  4. docker和kubernetes中hostname的使用和常见问题

    hostname在docker中是使用UTS namespace进行隔离的.docker中主要有两种ns的用法, 一种是docker run --uts="" busybox.这种 ...

  5. Pycharm在运行过程中,查看每个变量的方法(show variables)跟终端一样显示变量

    点击运行栏的这个灰色向下剪头: 在出现的窗口上,勾选上: 点击OK,重启Pycharm:接着点击Run窗口: 将Run的show variables图标勾选: 然后你就会发现,在右边出现了变量的窗口:

  6. [ZOJ 4016] Mergable Stack

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...

  7. 最大子段和的DP算法设计及其效率测试

    表情包形象取自番剧<猫咪日常> 那我也整一个 曾几何时,笔者是个对算法这个概念漠不关心的人,由衷地感觉它就是一种和奥数一样华而不实的存在,即便不使用任何算法的思想我一样能写出能跑的程序 直 ...

  8. eclipse报错:Multiple annotations found at this line: - String cannot be resolved to a type解决方法实测

    Multiple annotations found at this line:- String cannot be resolved to a type- The method getContext ...

  9. 【GIT】【命令行】

    1) 进入某没有做git的目录: git init ->username@DESKTOP-765V31O MINGW64 ~/PycharmProjects/user_data (master) ...

  10. Unity日常记录 - QualitySettings 性能设置

    unity打包时,可通过QualitySettings优化图像性能,这是最常设置也是最明显的图像性能体现 设置图形质量的水平,一般来说,质量是以牺牲性能为代价的,所以最好不要追求移动设备或旧硬件的最高 ...