XML Helper XML操作类
写的一个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>
/**************************************************
* 使用示列:
* 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)
{
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(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>
/**************************************************
* 使用示列:
* 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 { }
}
}
}
==================================================
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.jecda.com);
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", http://www.jecda.com);
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", http://www.jecda.com);
//修改元素值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
//修改属性值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", http://www.jecda.com);
//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", "");
XML Helper XML操作类的更多相关文章
- XML转换为对象操作类详解
//XML转换为对象操作类 //一,XML与Object转换类 using System.IO; using System.Runtime.Serialization.Formatters.Binar ...
- 关于XML文档操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- C#操作Xml树的扩展类
本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- Xml通用操作类
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...
- c# xml操作类 比较齐全
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...
- C#对XML操作类
C#对XML操作类 该类包含了对XML文件的创建,添加,读取,删除,修改等操作 //#define isUnity #if isUnity using UnityEngine; #endif usin ...
随机推荐
- win7和centos双系统安装
几年之前为了安装xp和linux的双系统曾折腾了好多天,今天为了安装这个win7和centos双系统,也折腾了两天多,哦,我的天,安装个双系统,怎么这么麻烦呢? 没有来得及整理,先铺上草稿,供同志们参 ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- C# 中如何判断某个字符串是否为空的方法 分享了三个方法来判断字
1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool isEmpty = (str == String.Empty ...
- Java——泛型(最易懂的方式讲解泛型)
来自: 代码大湿 代码大湿 写在前面: 只要认真看过,基本能很熟悉泛型的特性.泛型是JDK1.5之后出现的,比如JDK1.5之前的ArrayList,会出现2个问题 1:向ArrayList当中添加对 ...
- Calculating a bearing between points in location-aware apps
https://software.intel.com/en-us/blogs/2012/11/30/calculating-a-bearing-between-points-in-location-a ...
- 【现代程序设计】【homework-08】
1. 理解C++变量的作用域和生命周期 #include<stdio.h> char * test() { ]="; return s; } main() { puts(test ...
- [iOS UI进阶 - 6.1] 核心动画CoreAnimation
A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...
- javascript 获取HTML DOM父、子、临近节点
在Web应用程序特别是Web2.0程序开发中,经常要获取页面中某个元素,然后更新该元素的样式.内容等.如何获取要更新的元素,是首先要解决的问题.令人欣慰的是,使用JavaScript获取节点的方法有很 ...
- CodeForces 706C Hard problem (水DP)
题意:对于给定的n个字符串,可以花费a[i] 将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...
- android使用mount挂载/system/app为读写权限,删除或替换系统应用
注意:以下代码中#开头的则为需要执行的shell命令,其他的为打印的结果.#代表需要使用ROOT权限(su)执行,所以想要修改您android手机某个目录挂载为读写,首先需要有ROOT权限! 先要得到 ...