当前位置:网站首页>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;
}
}
}
边栏推荐
- HCM 初学 ( 四 ) - 时间
- el-table 动态表头渲染 固定第一列 高度问题
- Essay learning record essay multi label Global
- Code shoe set - mt3114 · interesting balance - explain it with examples
- MySQL数据迁移遇到的一些错误
- Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
- Chip, an empire built on sand!
- OpenGL ES: (4) EGL API详解 (转)
- Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
- He struggled day and night to protect his data
猜你喜欢

【笔记】电商订单数据分析实战

π disk, turning your computer into a personal private cloud

3D printer threading: five simple solutions

PLA not pasted on the bed: 6 simple solutions

我从技术到产品经理的几点体会

Small guide for rapid completion of mechanical arm (VI): stepping motor driver

从诺奖知“边缘计算”的未来!

scope 数据导出mat

4GB大文件,如何实时远程传输和共享?

2022 年面向初学者的 10 大免费 3D 建模软件
随机推荐
Looking for high school student developers with similar interests
数据库er图组成要素
TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
Servlet
Orcle创建用户+角色
扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
Codeforces Round #803 (Div. 2)vp
Some errors encountered in MySQL data migration
Pla ne colle pas sur le lit: 6 solutions simples
He struggled day and night to protect his data
Thoughts on a "01 knapsack problem" expansion problem
DEV XPO对比之UOW
kotlin位运算的坑(bytes[i] and 0xff 报错)
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
[note] e-commerce order data analysis practice
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
MySQL里记录货币
srpingboot security demo
excel初级应用案例——杜邦分析仪
如何添加葫芦儿派盘