你的位置:首页 > 网页设计

[网页设计]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.

namespace PuTianCheng
{
    /// <summary>
    ///     /// </summary>
    public class     {
        public         {
        }

        /// <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
            {
                                doc.Load(path);
                                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
            {
                                doc.Load(path);
                                if (element.Equals(""))
                {
                    if (!attribute.Equals(""))
                    {
                                                xe.SetAttribute(attribute, value);
                    }
                }
                else
                {
                                        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
            {
                                doc.Load(path);
                                                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
            {
                                doc.Load(path);
                                                if (attribute.Equals(""))
                    xn.ParentNode.RemoveChild(xn);
                else
                    xe.RemoveAttribute(attribute);
                doc.Save(path);
            }
            catch { }
        }
    }
}

==================================================


<?<Root />

==================================================

使用方法:

string //插入元素
////插入元素/属性
//////////插入属性
////////修改元素值
////修改属性值
//////读取元素值
//Response.Write("<div>" + //读取属性值
//Response.Write("<div>" + //读取特定属性值
//Response.Write("<div>" + //删除属性
////删除元素
//