当前位置:网站首页>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;
}
}
}
边栏推荐
- Crossing pie · pie pan + Mountain duck = local data management
- How to add a gourd pie plate
- SQL必会题之留存率
- Dear pie users, I want to confess to you!
- Qt编写自定义控件-自绘电池
- 解决麒麟V10上传文件乱码问题
- Primary application case of Excel DuPont analyzer
- 2022 the 8th China International "Internet +" college student innovation and entrepreneurship competition industry proposition track is open for registration!
- OneFlow源码解析:算子签名的自动推断
- Preliminary level of C language -- selected good questions on niuke.com
猜你喜欢

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

Advanced cross platform application development (III): online resource upgrade / hot update with uni app

He struggled day and night to protect his data

Database problems, how to optimize Oracle SQL query statements faster and more efficient

On the first day of the new year, 3000 Apache servers went down

uniapp树形层级选择器

论文学习记录随笔 多标签之LIFT

excel動態圖錶

Scope data export mat

Why use huluer pie disk instead of U disk?
随机推荐
This is the necessary software for college students 𞓜 knowledge management
我从技术到产品经理的几点体会
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
Fragment upload and breakpoint resume
Build 2022 上开发者最应关注的七大方向主要技术更新
Call us special providers of personal cloud services for College Students
π disk, turning your computer into a personal private cloud
分片上传与断点续传
Some errors encountered in MySQL data migration
Oracle sequence + trigger
HCM 初学 ( 二 ) - 信息类型
OpenGL ES: (1) OpenGL ES的由来 (转)
PLA不粘貼在床上:6個簡單的解决方案
jdbc 数据库操作
OpenGL es: (4) detailed explanation of EGL API (Continued)
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
Know the future of "edge computing" from the Nobel prize!
Brief description of activation function