xml 帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

public class XmlDoc
    {
         XmlDocument doc;
         string xmlFileName;
         readonly string xmlRootName;
        public XmlNode xmlRoot;

        //设置XML文件名
        public string FileName
        {
            set { xmlFileName = HttpContext.Current.Server.MapPath(value); }
        }

        /// <summary>
        /// 类初始化
        /// </summary>
        /// <param name='path'>文件夹</param>
        /// <param name='filename'>xml文件名</param>
        /// <param name='root'>xml根节点</param>
        public XmlDoc(string path, string filename, string root)
        {
            xmlRootName = root;
            xmlFileName = HttpContext.Current.Server.MapPath(path) + "\\" + filename;
            doc = new XmlDocument();
            try
            {
                doc.Load(xmlFileName);
            }
            catch
            {
                doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><" + root + "></" + root + ">");
            }
            xmlRoot = doc.SelectSingleNode(xmlRootName);
        }

        /// <summary>
        /// 类初始化(从内容实例化XML)
        /// </summary>
        /// <param name='Content'>内容</param>
        /// <param name='root'>xml根节点</param>
        public XmlDoc(string Content, string root)
        {
            xmlRootName = root;
            doc = new XmlDocument();
            try
            {
                doc.LoadXml(Content);
            }
            catch
            {
                doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><" + xmlRootName + "></" + xmlRootName + ">");
            }
            xmlRoot = doc.SelectSingleNode(xmlRootName);
            xmlFileName = HttpContext.Current.Server.MapPath(root + ".xml");
        }

        /// <summary>
        /// 保存XML文件
        /// </summary>
        /// <returns></returns>
        public void Save()
        {
            try
            {
                string server_path = Helper.GetServerPath(xmlFileName);
                if (!Directory.Exists(server_path))
                {
                    Directory.CreateDirectory(server_path);
                }
                doc.Save(xmlFileName);
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 删除XML文件
        /// </summary>
        /// <returns></returns>
        public void Delete()
        {
            try
            {
                if (File.Exists(xmlFileName))
                {
                    File.Delete(xmlFileName);
                }
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 获取类里的XmlDocument
        /// </summary>
        /// <returns></returns>
        public XmlDocument Doc
        {
            get { return doc; }
        }

        /// <summary>
        /// 对类里的XmlDocument重新初始化
        /// </summary>
        /// <returns></returns>
        public void DocInit()
        {
            doc = new XmlDocument();
            doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><" + xmlRoot + "></" + xmlRoot + ">");
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <param name='IsAutoCreate'>是否自动创建</param>
        /// <returns>XmlElement</returns>
        public XmlElement GetXmlNodeByTagNameAndIndex(XmlElement parentNode, string tagName, int index, bool IsAutoCreate)
        {
            XmlElement xe;
            if (index < 0)
            {
                return null;
            }
            try
            {
                XmlNodeList elemList = parentNode.SelectNodes(tagName);
                //自动创建
                if (elemList.Count <= index && IsAutoCreate)
                {
                    for (int i = elemList.Count; i <= index; i++)
                    {
                        parentNode.AppendChild(doc.CreateElement(tagName));
                    }
                    elemList = parentNode.SelectNodes(tagName);
                }
                XmlNode xn = elemList[index];
                xe = (XmlElement)xn;
            }
            catch
            {
                xe = null;
            }
            return xe;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement(根据节点属性值查找)
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <param name='AttributeName'>节点属性名称</param>
        /// <param name='AttributeValue'>节点属性值</param>
        /// <param name='IsAutoCreate'>是否自动创建</param>
        /// <returns>XmlElement</returns>
        public XmlElement GetXmlNodeByTagNameAndAttribute(XmlElement parentNode, string tagName, string AttributeName, string AttributeValue, bool IsAutoCreate)
        {
            bool IsExists = false;
            XmlElement xe = null;
            string Value = "";
            try
            {
                XmlNodeList elemList = parentNode.SelectNodes(tagName);
                if (elemList != null)
                {
                    foreach (XmlNode xn in elemList)
                    {
                        if (xn.NodeType == XmlNodeType.Element)
                        {
                            XmlElement xel = (XmlElement)xn;
                            if (xel.HasAttribute(AttributeName))
                            {
                                Value = xel.GetAttribute(AttributeName);
                            }
                            if (Value == AttributeValue)
                            {
                                IsExists = true;
                                xe = xel;
                                break;
                            }
                        }
                    }
                }
                //自动创建
                if (!IsExists && IsAutoCreate)
                {
                    XmlElement elem = doc.CreateElement(tagName);
                    elem.SetAttribute(AttributeName, AttributeValue);
                    parentNode.AppendChild(elem);
                    xe = elem;
                }
            }
            catch
            {
                xe = null;
            }
            return xe;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement(根节点作父节点)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <param name='IsAutoCreate'>是否自动创建</param>
        /// <returns>XmlElement</returns>
        public XmlElement GetXmlNodeByTagNameAndIndex(string tagName, int index, bool IsAutoCreate)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetXmlNodeByTagNameAndIndex(ParentNode, tagName, index, IsAutoCreate);
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement(根节点作父节点,根据节点属性值查找)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <param name='AttributeName'>节点属性名称</param>
        /// <param name='AttributeValue'>节点属性值</param>
        /// <param name='IsAutoCreate'>是否自动创建</param>
        /// <returns>XmlElement</returns>
        public XmlElement GetXmlNodeByTagNameAndAttribute(string tagName, string AttributeName, string AttributeValue, bool IsAutoCreate)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetXmlNodeByTagNameAndAttribute(ParentNode, tagName, AttributeName, AttributeValue, IsAutoCreate);
        }

        /// <summary>
        /// 为类里的XmlDocument一个特定XmlElement设置子CData节点
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <param name='tagValue'>子节点内容</param>
        /// <returns></returns>
        public void SetXmlCDataNode(XmlElement xe, string tagName, string tagValue)
        {
            XmlNode elemList = xe.SelectSingleNode(tagName);
            if (elemList != null)
            {
                if (elemList.FirstChild == null)
                {
                    XmlNode elem = elemList.AppendChild(doc.CreateCDataSection(tagValue));
                    elemList.AppendChild(elem);
                }
                else
                {
                    elemList.FirstChild.Value = tagValue;
                }
            }
            else
            {
                XmlElement elem = doc.CreateElement(tagName);
                XmlNode xn = elem.AppendChild(doc.CreateCDataSection(tagValue));
                elem.AppendChild(xn);
                xe.AppendChild(elem);
            }
        }

        /// <summary>
        /// 为类里的XmlDocument一个特定XmlElement设置子CData节点(根节点作节点)
        /// </summary>
        /// <param name='tagName'>子节点名称</param>
        /// <param name='tagValue'>子节点内容</param>
        /// <returns></returns>
        public void SetXmlCDataNode(string tagName, string tagValue)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            SetXmlCDataNode(ParentNode, tagName, tagValue);
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子CData节点内容
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <returns>string</returns>
        public string GetXmlCData(XmlElement xe, string tagName)
        {
            string tagValue = "";
            XmlNode elemList = xe.SelectSingleNode(tagName);
            if (elemList != null)
            {
                if (elemList.FirstChild != null)
                {
                    tagValue = elemList.FirstChild.Value;
                }
            }
            return tagValue;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子CData节点内容(根节点作节点)
        /// </summary>
        /// <param name='tagName'>子节点名称</param>
        /// <returns>string</returns>
        public string GetXmlCData(string tagName)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetXmlCData(ParentNode, tagName);
        }

        /// <summary>
        /// 为类里的XmlDocument一个特定XmlElement设置属性
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>节点属性名称</param>
        /// <param name='tagValue'>节点属性值</param>
        /// <returns></returns>
        public void SetXmlAttribute(XmlElement xe, string tagName, string tagValue)
        {
            if (xe.HasAttribute(tagName))
            {
                xe.SetAttribute(tagName, tagValue);
            }
            else
            {
                xe.SetAttribute(tagName, tagValue);
            }
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的属性值
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>节点属性名称</param>
        /// <returns>string</returns>
        public string GetXmlAttribute(XmlElement xe, string tagName)
        {
            string tagValue = "";
            if (xe.HasAttribute(tagName))
            {
                tagValue = xe.GetAttribute(tagName);
            }
            return tagValue;
        }

        /// <summary>
        /// 移除类里的XmlDocument一个特定XmlElement的属性值
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>节点属性名称</param>
        /// <returns></returns>
        public void RemoveXmlAttribute(XmlElement xe, string tagName)
        {
            if (xe.HasAttribute(tagName))
            {
                xe.RemoveAttribute(tagName);
            }
        }

        /// <summary>
        /// 移除类里的XmlDocument一个特定XmlElement的子节点
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <returns></returns>
        public void RemoveXmlChildNode(XmlElement xe, string tagName)
        {
            XmlNode removeNode = xe.SelectSingleNode(tagName);
            if (removeNode != null)
            {
                xe.RemoveChild(removeNode);
            }
        }

        /// <summary>
        /// 移除类里的XmlDocument一个特定XmlElement(根节点不能移除)
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <returns></returns>
        public void RemoveXmlNode(XmlElement xe)
        {
            if (xe.ParentNode != null)
            {
                xe.ParentNode.RemoveChild(xe);
            }
        }

        /// <summary>
        /// 上移类里的XmlDocument一个特定XmlElement
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns></returns>
        public void Up(XmlElement parentNode, string tagName, int index)
        {
            XmlNodeList elemList = parentNode.SelectNodes(tagName);
            if (elemList != null)
            {
                if ((elemList.Count) > index && (index > 0))
                {
                    XmlNode refChild = elemList[index];

                    XmlNode xn = elemList[index - 1];
                    RemoveXmlNode((XmlElement)xn);
                    parentNode.InsertAfter(xn, refChild);
                }
            }
        }

        /// <summary>
        /// 上移类里的XmlDocument一个特定XmlElement(根节点作父节点)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns></returns>
        public void Up(string tagName, int index)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            Up(ParentNode, tagName, index);
        }

        /// <summary>
        /// 下移类里的XmlDocument一个特定XmlElement
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns></returns>
        public void Down(XmlElement parentNode, string tagName, int index)
        {
            XmlNodeList elemList = parentNode.SelectNodes(tagName);
            if (elemList != null)
            {
                if ((elemList.Count - 1) > index && (index >= 0))
                {
                    XmlNode refChild = elemList[index];

                    XmlNode xn = elemList[index + 1];
                    RemoveXmlNode((XmlElement)xn);
                    parentNode.InsertBefore(xn, refChild);
                }
            }
        }

        /// <summary>
        /// 下移类里的XmlDocument一个特定XmlElement(根节点作父节点)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns></returns>
        public void Down(string tagName, int index)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            Down(ParentNode, tagName, index);
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement下的所有相同节点名的节点数量
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <returns>int</returns>
        public int GetNodeCount(XmlElement parentNode, string tagName)
        {
            int NodeCount = 0;
            XmlNodeList elemList = parentNode.SelectNodes(tagName);
            if (elemList != null)
            {
                NodeCount = elemList.Count;
            }
            return NodeCount;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement下的所有相同节点名的节点数量(根节点作父节点)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <returns>int</returns>
        public int GetNodeCount(string tagName)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetNodeCount(ParentNode, tagName);
        }

        /// <summary>
        /// 为类里的XmlDocument一个特定XmlElement设置子Text节点
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <param name='tagValue'>子节点内容</param>
        /// <returns></returns>
        public void SetXmlTextNode(XmlElement xe, string tagName, string tagValue)
        {
            XmlNode elemList = xe.SelectSingleNode(tagName);
            if (elemList != null)
            {
                if (elemList.FirstChild == null)
                {
                    XmlNode elem = elemList.AppendChild(doc.CreateTextNode(tagValue));
                    elemList.AppendChild(elem);
                }
                else
                {
                    elemList.FirstChild.Value = tagValue;
                }
            }
            else
            {
                XmlElement elem = doc.CreateElement(tagName);
                XmlNode xn = elem.AppendChild(doc.CreateTextNode(tagValue));
                elem.AppendChild(xn);
                xe.AppendChild(elem);
            }
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <returns>string</returns>
        public string GetXmlTextNode(XmlElement xe, string tagName)
        {
            string tagValue = "";
            XmlNode elemList = xe.SelectSingleNode(tagName);
            if (elemList != null)
            {
                if (elemList.FirstChild != null)
                {
                    tagValue = elemList.FirstChild.Value;
                }
            }
            return tagValue;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>子节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns>string</returns>
        public string GetXmlText(XmlElement xe, string tagName, int index)
        {
            string tagValue = "";
            XmlNodeList elemList = xe.SelectNodes(tagName);
            if (elemList != null)
            {
                if ((elemList.Count) > index)
                {
                    XmlNode refChild = elemList[index];
                    if (refChild.FirstChild != null)
                    {
                        tagValue = refChild.FirstChild.Value;
                    }
                }
            }
            return tagValue;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容(根节点作节点)
        /// </summary>
        /// <param name='tagName'>子节点名称</param>
        /// <param name='index'>节点数组下标值</param>
        /// <returns>string</returns>
        public string GetXmlText(string tagName, int index)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetXmlText(ParentNode, tagName, index);
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的子Text节点内容(根节点作节点)
        /// </summary>
        /// <param name='tagName'>子节点名称</param>
        /// <returns>string</returns>
        public string GetXmlTextNode(string tagName)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetXmlTextNode(ParentNode, tagName);
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的所有有特定属性的节点的属性的最大值(属性值必须是INT型)
        /// </summary>
        /// <param name='parentNode'>父节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <param name='AttributeName'>节点属性名称</param>
        /// <returns>int</returns>
        public int GetMaxValueByTagNameAndAttribute(XmlElement parentNode, string tagName, string AttributeName)
        {
            int MaxTagValue = 1;
            XmlNodeList elemList = parentNode.SelectNodes(tagName);
            if (elemList != null)
            {
                foreach (XmlNode xn in elemList)
                {
                    if (xn.NodeType == XmlNodeType.Element)
                    {
                        XmlElement xel = (XmlElement)xn;
                        if (xel.HasAttribute(AttributeName))
                        {
                            int iValue = FormChecker.IsNumeric(xel.GetAttribute(AttributeName), 0);
                            if (iValue >= MaxTagValue)
                            {
                                MaxTagValue = iValue + 1;
                            }
                        }
                    }
                }
            }
            return MaxTagValue;
        }

        /// <summary>
        /// 获取类里的XmlDocument一个特定XmlElement的所有有特定属性的节点的属性的最大值(属性值必须是INT型,根节点作节点)
        /// </summary>
        /// <param name='tagName'>节点名称</param>
        /// <param name='AttributeName'>节点属性名称</param>
        /// <returns>int</returns>
        public int GetMaxValueByTagNameAndAttribute(string tagName, string AttributeName)
        {
            XmlElement ParentNode = (XmlElement)xmlRoot;
            return GetMaxValueByTagNameAndAttribute(ParentNode, tagName, AttributeName);
        }

        /// <summary>
        /// 判断类里的XmlDocument一个特定XmlElement是否存在特定节点
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <returns>int</returns>
        public bool HasAttribute(XmlElement xe, string tagName)
        {
            return xe.HasAttribute(tagName);
        }
        /// <summary>
        /// 获取特定XmlElement的节点值
        /// </summary>
        /// <param name='xe'>节点</param>
        /// <param name='tagName'>节点名称</param>
        /// <returns>string</returns>
        public string GetXmlCDataNode(XmlElement xe, string tagName)
        {
            string tagValue = "";
            XmlNode elemList = xe.SelectSingleNode(tagName);
            if (elemList != null)
            {
                if (elemList.FirstChild != null)
                {
                    tagValue = elemList.FirstChild.Value;
                }
            }
            return tagValue;
        }

        /// <summary>
        /// 获取根节点下特定XmlElement的节点值
        /// </summary>
        /// <param name='itemName'>节点名</param>
        /// <param name='tagName'>节点属性名称</param>
        /// <param name='tagValue'>节点属性值</param>
        /// <returns>string</returns>
        public string GetXmlTextNodeByAttribute(string itemName, string tagName, string tagValue)
        {
            string value = "";
            XmlElement xe = GetXmlNodeByTagNameAndAttribute(itemName, tagName, tagValue, false);
            if (xe != null)
            {
                value = GetXmlCDataNode(xe, "value");

            }
            if (value == "")
            {
                value = tagValue;
            }
            return value;
        }
    }
posted @ 2011-07-13 17:01  小川丶  阅读(392)  评论(0编辑  收藏  举报