欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的XML操作类实例

程序员文章站 2023-12-06 12:23:34
本文实例讲述了c#实现的xml操作类。分享给大家供大家参考。具体如下: 这里讲述了c#写的一个xml操作类,包括读取/插入/修改/删除。 using syste...

本文实例讲述了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#程序设计有所帮助。