C#实现的XML操作类实例
作者:软件工程师 时间:2022-04-04 12:17:37
本文实例讲述了C#实现的XML操作类。分享给大家供大家参考。具体如下:
这里讲述了C#写的一个XML操作类,包括读取/插入/修改/删除。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace PuTianCheng
{
/// <summary>
/// XmlHelper 的摘要说明
/// </summary>
public class XmlHelper
{
public XmlHelper()
{
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
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>
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
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_x(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
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>
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 { }
}
}
}
XmlFile.xml:
<?xml version="1.0" encoding="utf-8"?>
<Root />
使用方法:
string xml = Server.MapPath("XmlFile.xml");
//插入元素
XmlHelper.Insert(xml, "/Root", "Studio", "", "");
//插入元素/属性
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
//插入属性
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
//修改元素值
XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
//修改属性值
XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
//读取元素值
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
//读取属性值
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
//读取特定属性值
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</div>");
//删除属性
XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
//删除元素
XmlHelper.Delete(xml, "/Root/Studio", "");
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,XML
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
AOP之事务管理<aop:advisor>的两种配置方式
2023-11-24 22:55:06
flutter material widget组件之信息展示组件使用详解
2023-06-22 08:45:35
字符串内存驻留机制详解示例
2023-09-24 04:09:32
Spring MVC学习教程之视图深入解析
2021-12-16 23:37:55
Java并发编程预防死锁过程详解
2023-11-09 15:33:58
一文带你了解Java选择排序的原理与实现
2022-05-13 21:01:31
![](https://img.aspxhome.com/file/2023/4/64784_0s.jpg)
Spring Security基本原理详解
2022-12-24 15:57:54
![](https://img.aspxhome.com/file/2023/9/62459_0s.jpg)
JDK 7 新特性小结实例代码解析
2022-04-18 13:03:07
使用controller传boolean形式值
2023-11-28 23:05:33
Gradle的安装和环境变量的配置详解
2023-07-11 04:56:37
![](https://img.aspxhome.com/file/2023/2/83552_0s.png)
使用JAVA实现http通信详解
2023-11-12 12:21:12
![](https://img.aspxhome.com/file/2023/4/61924_0s.png)
一次由Lombok的@AllArgsConstructor注解引发的错误及解决
2023-10-07 05:23:42
![](https://img.aspxhome.com/file/2023/3/58683_0s.png)
Java实现PDF转为Word文档的示例代码
2021-10-05 02:19:26
![](https://img.aspxhome.com/file/2023/7/62657_0s.jpg)
flutter实现倒计时加载页面
2023-08-18 23:30:09
![](https://img.aspxhome.com/file/2023/1/109171_0s.gif)
java整合SSM框架的图文教程
2023-03-11 01:06:30
![](https://img.aspxhome.com/file/2023/8/67168_0s.jpg)
SpringBoot集成MyBatis的分页插件PageHelper实例代码
2023-09-11 21:54:33
Mybatis-Plus注入SQL原理分析
2022-11-09 21:17:22
![](https://img.aspxhome.com/file/2023/6/64496_0s.jpg)
Java客户端调用.NET的WebService实例
2023-11-03 17:22:00
![](https://img.aspxhome.com/file/2023/8/58988_0s.png)
完美解决Spring Boot前端的Access-Control-Allow-Origin跨域问题
2023-09-19 10:16:28
java实现简单计算器功能
2021-06-17 11:21:23
![](https://img.aspxhome.com/file/2023/2/74772_0s.jpg)