当前位置:编程学习 > XML/UML >>

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;
              

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,