当前位置:网站首页>C XML help class
C XML help class
2022-07-01 06:00:00 【Earn money and spend 388】
For reading or creating xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace RestSOE102
{
/// <summary>
/// Xml Helper classes
/// </summary>
public class XmlHelper
{
/// <summary>
/// Reading data
/// </summary>
/// <param name="path"> route </param>
/// <param name="node"> node </param>
/// <param name="attribute"> Property name , Return the property value if it is not empty , Otherwise, the concatenation value is returned </param>
/// <returns>string</returns>
/**************************************************
* Use list :
* XmlHelper.Read(path, "/Node", "")
* XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
************************************************/
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
}
/// <summary>
/// insert data
/// </summary>
/// <param name="path"> route </param>
/// <param name="node"> node </param>
/// <param name="element"> Element name , Insert a new element when it is not empty , Otherwise, insert the attribute in the element </param>
/// <param name="attribute"> Property name , Insert the attribute value of the element if it is not empty , Otherwise insert the element value </param>
/// <param name="value"> value </param>
/// <returns></returns>
/**************************************************
* Use list :
* XmlHelper.Insert(path, "/Node", "Element", "", "Value")
* XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
* XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
************************************************/
public static void Insert(string path, string node, string element, string attribute, string value)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
/// <summary>
/// Modifying data
/// </summary>
/// <param name="path"> route </param>
/// <param name="node"> node </param>
/// <param name="attribute"> Property name , Modify the attribute value of this node if it is not empty , Otherwise, modify the node value </param>
/// <param name="value"> value </param>
/// <returns></returns>
/**************************************************
* Use list :
* XmlHelper.Insert(path, "/Node", "", "Value")
* XmlHelper.Insert(path, "/Node", "Attribute", "Value")
************************************************/
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
doc.Save(path);
}
catch { }
}
/// <summary>
/// Delete data
/// </summary>
/// <param name="path"> route </param>
/// <param name="node"> node </param>
/// <param name="attribute"> Property name , Delete the attribute value of this node if it is not empty , Otherwise, delete the node value </param>
/// <param name="value"> value </param>
/// <returns></returns>
/**************************************************
* Use list :
* XmlHelper.Delete(path, "/Node", "")
* XmlHelper.Delete(path, "/Node", "Attribute")
************************************************/
public static void Delete(string path, string node, string attribute)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xn.ParentNode.RemoveChild(xn);
else
xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch { }
}
/// <summary>
/// Specify the number of nodes
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <returns></returns>
public static int Notecout(string path, string node)
{
int count = 0;
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList xnList = doc.SelectNodes("//" + node);
return count = xnList.Count;
}
/// <summary>
/// Get the value of multiple nodes with the same name
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <returns></returns>
public static List<string> vaulelist(string path, string node)
{
List<string> vlist = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList xnList = doc.SelectNodes("//" + node);
foreach (XmlNode xn in xnList)
{
XmlElement xe = (XmlElement)xn;
string value = xe.InnerText;
vlist.Add(value);
}
return vlist;
}
}
}
边栏推荐
猜你喜欢

PLA不粘貼在床上:6個簡單的解决方案

不是你脑子不好用,而是因为你没有找到对的工具

Dear pie users, I want to confess to you!

解决麒麟V10上传文件乱码问题

Why use huluer pie disk instead of U disk?

el-table 动态表头渲染 固定第一列 高度问题

C语言初阶——牛客网精选好题

excel动态图表

OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow

Preliminary level of C language -- selected good questions on niuke.com
随机推荐
这才是大学生必备软件 | 知识管理
喊我们大学生个人云服务特供商
My experience from technology to product manager
Excel dynamic chart
Primary application case of Excel DuPont analyzer
Talking from mlperf: how to lead the next wave of AI accelerator
Timer based on LabVIEW
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
穿越派 你的数据云行
Database problems, how to optimize Oracle SQL query statements faster and more efficient
Fragment upload and breakpoint resume
Seven major technical updates that developers should pay most attention to on build 2022
excel初级应用案例——杜邦分析仪
El tooltip in the table realizes line breaking display
Thoughts on a "01 knapsack problem" expansion problem
扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
SystemVerilog学习-08-随机约束和线程控制
【文件系统】如何在ubi之上运行squashfs
MySQL里记录货币
restframework-simpleJWT重写认证机制