当前位置:网站首页>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;
}
}
}
边栏推荐
- 4GB大文件,如何实时远程传输和共享?
- 解决麒麟V10上传文件乱码问题
- Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
- 指数法和Random Forest实现山东省丰水期地表水体信息
- srpingboot security demo
- Thesis learning record essay multi label lift
- Qt编写自定义控件-自绘电池
- SystemVerilog学习-06-类的封装
- SQL必会题之留存率
- My experience from technology to product manager
猜你喜欢
![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)
[note] e-commerce order data analysis practice

Call us special providers of personal cloud services for College Students

jdbc 数据库操作

Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)

HCM 初学 ( 二 ) - 信息类型

Seven major technical updates that developers should pay most attention to on build 2022

El tooltip in the table realizes line breaking display

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

Qt编写自定义控件-自绘电池

穿越派与贸大合作,为大学生增添效率
随机推荐
2022 the 8th China International "Internet +" college student innovation and entrepreneurship competition industry proposition track is open for registration!
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
论文学习记录随笔 多标签之GLOCAL
穿越派·派盘 + Mountain Duck = 数据本地管理
three.js小结
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
3D printer threading: five simple solutions
扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
uniapp树形层级选择器
excel動態圖錶
论文学习记录随笔 多标签之LSML
Dear pie users, I want to confess to you!
Send you through the data cloud
srpingboot security demo
OpenGL ES: (4) EGL API详解 (转)
MySQL里记录货币
Thesis learning record essay multi label lift
Code shoe set - mt3114 · interesting balance - explain it with examples
Thoughts on a "01 knapsack problem" expansion problem
OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow