当前位置:网站首页>c# Xml帮助类
c# Xml帮助类
2022-07-01 05:58:00 【挣钱花388】
用于读取或创建xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace RestSOE102
{
/// <summary>
/// Xml帮助类
/// </summary>
public class XmlHelper
{
/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/**************************************************
* 使用示列:
* 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>
/// 插入数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* 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>
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* 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>
/// 删除数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* 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>
/// 指定节点的个数
/// </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>
/// 获取指定相同名称的多个节点的值
/// </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;
}
}
}
边栏推荐
- 扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- It's not that you have a bad mind, but that you haven't found the right tool
- Skywalking integrated Nacos dynamic configuration
- HCM 初学 ( 二 ) - 信息类型
- Pla ne colle pas sur le lit: 6 solutions simples
- Multi label lsml for essay learning records
- Why use huluer pie disk instead of U disk?
- Huluer app help
- LED lighting used in health lighting
猜你喜欢

Call us special providers of personal cloud services for College Students

2022 年面向初学者的 10 大免费 3D 建模软件

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

亲爱的派盘用户,我要向您表白!
![[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved](/img/30/c802ae9b65601832bf52e760e9962d.png)
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved

excel動態圖錶

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432

Chip, an empire built on sand!

MySQL数据迁移遇到的一些错误

Database problems, how to optimize Oracle SQL query statements faster and more efficient
随机推荐
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved
论文学习记录随笔 多标签之GLOCAL
芯片,建立在沙粒上的帝国!
Preliminary level of C language -- selected good questions on niuke.com
Thoughts on a "01 knapsack problem" expansion problem
tese_ Time_ 2h
Chip, an empire built on sand!
Fixed height of the first column in El table dynamic header rendering
What if the data in the cloud disk is harmonious?
HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
excel可视化
OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系
这才是大学生必备软件 | 知识管理
freeswitch拨打分机号
穿越派·派盘 + Mountain Duck = 数据本地管理
Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
Flink实战--多流合并
Build 2022 上开发者最应关注的七大方向主要技术更新
码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
我从技术到产品经理的几点体会