当前位置:网站首页>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;
}
}
}
边栏推荐
- TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
- Codeforces Round #803 (Div. 2)vp
- 利用百度地图查询全国地铁线路
- bat操作ftp上传下载命令
- 指数法和Random Forest实现山东省丰水期地表水体信息
- OpenGL es: (2) relationship between OpenGL es, EGL and glsl
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- My experience from technology to product manager
- Chip, an empire built on sand!
- Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
猜你喜欢
3D printer threading: five simple solutions
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
Why use huluer pie disk instead of U disk?
Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
Primary application case of Excel DuPont analyzer
Thesis learning record essay multi label lift
4GB大文件,如何实时远程传输和共享?
HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
穿越派 你的数据云行
随机推荐
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
数据库er图组成要素
CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
HCM 初学 ( 四 ) - 时间
Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
π盘,让您电脑变成个人的私有云
穿越派与贸大合作,为大学生增添效率
穿越派·派盘 + 思源笔记 = 私人笔记本
不是你脑子不好用,而是因为你没有找到对的工具
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
[note] e-commerce order data analysis practice
srpingboot security demo
为了保护自己的数据,他奋斗了一天一夜
OpenGL es: (1) origin of OpenGL es (transfer)
uniapp树形层级选择器
excel初级应用案例——杜邦分析仪
[medical segmentation] u2net
restframework-simpleJWT重写认证机制
4GB大文件,如何实时远程传输和共享?
栈题目:解析布尔表达式