WebForm 处理Xml文件类

using System;
using System.Xml;
using System.Web;

namespace ZhUtility
{
    //弄清几个定义
    //节点XPath(NodeXPath)
    //节点名(NodeName)
    //节点值(NodeValue)
    //节点属性名(NodeAttributeName)
    //节点属性值(NodeAttributeValue)
    //根节点(RootNode)
    //父节点(ParentNode)
    //子节点(ChildNode)

    public class ControlXmlFile
    {
        //字段
        public enum enumXmlPathType
        {   
            AbsolutePath,
            VirtualPath
        }
        private string strXmlFilePath;
        private enumXmlPathType eXmlPathType;
        private XmlDocument myXmlDocument = new XmlDocument();


        //属性
        public string XmlFilePath
        {
            get
            {
                return strXmlFilePath;
            }
            set
            {
                strXmlFilePath = value;
            }
        }
        public enumXmlPathType XmlPathType
        {
            set
            {
                eXmlPathType = value;
            }
        }


        //构造函数
        public ControlXmlFile(string strTempXmlfilePath)
        {
            strXmlFilePath = strTempXmlfilePath;
            eXmlPathType = enumXmlPathType.VirtualPath;
            GetXmlDocument();
        }

        public ControlXmlFile(string strTempXmlfilePath, enumXmlPathType eTempXmlPathType)
        {
            strXmlFilePath = strTempXmlfilePath;
            eXmlPathType = eTempXmlPathType;
            GetXmlDocument();
        }

        private void GetXmlDocument()
        {
            //绝对路径
            if (eXmlPathType == enumXmlPathType.AbsolutePath)
            {
                //加载Xml文档
                myXmlDocument.LoadXml(strXmlFilePath);
            }
            //虚拟路径
            else if (eXmlPathType == enumXmlPathType.VirtualPath)
            {
                //加载Xml文档
                myXmlDocument.Load(HttpContext.Current.Server.MapPath(strXmlFilePath));
            }
            //添加事件处理程序
            myXmlDocument.NodeChanged += new XmlNodeChangedEventHandler(myXmlDocument_NodeChanged);//update
            myXmlDocument.NodeInserted += new XmlNodeChangedEventHandler(myXmlDocument_NodeInserted);//insert
            myXmlDocument.NodeRemoved += new XmlNodeChangedEventHandler(myXmlDocument_NodeRemoved);//delete
        }

        //update事件处理程序
        private void myXmlDocument_NodeChanged(object sender, XmlNodeChangedEventArgs e)
        {
            SaveXmlDocument();
        }

        //insert事件处理程序
        private void myXmlDocument_NodeInserted(object sender, XmlNodeChangedEventArgs e)
        {
            SaveXmlDocument();
        }

        //delete事件处理程序
        private void myXmlDocument_NodeRemoved(object sender, XmlNodeChangedEventArgs e)
        {
            SaveXmlDocument();
        }

        //保存Xml文档(覆盖)
        public void SaveXmlDocument()
        {
            try
            {
                //绝对路径
                if (eXmlPathType == enumXmlPathType.AbsolutePath)
                {
                    SaveXmlDocument(strXmlFilePath);
                }
                //虚拟路径
                else if (eXmlPathType == enumXmlPathType.VirtualPath)
                {
                    SaveXmlDocument(HttpContext.Current.Server.MapPath(strXmlFilePath));
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //保存Xml文档(另存为)
        public void SaveXmlDocument(string strAnotherXmlFilePath)
        {
            try
            {
                myXmlDocument.Save(strAnotherXmlFilePath);
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //读取节点值 
        public string GetNodeValue(string strNodeXPath)
        {
            string strReturn = string.Empty;
            try
            {
                XmlNode myXmlNode = myXmlDocument.SelectSingleNode(strNodeXPath);
                if (myXmlNode != null)
                {
                    strReturn = myXmlNode.InnerText;
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
            return strReturn;
        }

        //读取节点属性值   
        public string GetNodeAttributeValue(string strNodeXPath, string strAttributeName)
        {
            string strReturn = string.Empty;
            try
            {
                XmlNode myXmlNode = myXmlDocument.SelectSingleNode(strNodeXPath);
                if (myXmlNode != null)
                {
                    //获取节点的属性,并循环取出需要的属性值
                    XmlAttributeCollection myXmlAttributeCollection = myXmlNode.Attributes;
                    for (int i = 0; i < myXmlAttributeCollection.Count; i++)
                    {
                        if (myXmlAttributeCollection.Item(i).Name == strAttributeName)
                        {
                            strReturn = myXmlAttributeCollection.Item(i).Value;
                            break;
                        }
                    }
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
            return strReturn;
        }

        //设置节点值(update)
        public void SetNodeValue(string strNodeXPath, string strNodeValue)
        {
            try
            {
                XmlNode myXmlNode = myXmlDocument.SelectSingleNode(strNodeXPath);
                if (myXmlNode != null)
                {
                    myXmlNode.InnerText = strNodeValue;
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //设置节点属性值(update)
        public void SetNodeAttributeValue(string strNodeXPath, string strAttributeName, string strAttributeValue, bool bIsSingle)
        {
            try
            {
                //只处理匹配的第一个Node
                if (bIsSingle)
                {
                    XmlNode myXmlNode = myXmlDocument.SelectSingleNode(strNodeXPath);
                    if (myXmlNode != null)
                    {
                        //获取节点的属性,并循环取出需要的属性值
                        XmlAttributeCollection myXmlAttributeCollection = myXmlNode.Attributes;
                        for (int i = 0; i < myXmlAttributeCollection.Count; i++)
                        {
                            if (myXmlAttributeCollection.Item(i).Name == strAttributeName)
                            {
                                myXmlAttributeCollection.Item(i).Value = strAttributeValue;
                                break;
                            }
                        }
                    }
                }
                //处理所有匹配的Node
                else
                {
                    XmlNodeList myXmlNodeList = myXmlDocument.SelectNodes(strNodeXPath);
                    if (myXmlNodeList != null)
                    {
                        foreach (XmlNode myXmlNode in myXmlNodeList)
                        {
                            XmlAttributeCollection myXmlAttributeCollection = myXmlNode.Attributes;
                            for (int i = 0; i < myXmlAttributeCollection.Count; i++)
                            {
                                if (myXmlAttributeCollection.Item(i).Name == strAttributeName)
                                {
                                    myXmlAttributeCollection.Item(i).Value = strAttributeValue;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //向根节点中添加一个父节点(insert)
        public void AddParentNode(string strParentNodeName)
        {
            try
            {
                XmlNode myXmlNodeRoot = myXmlDocument.DocumentElement;//获得文档的根
                XmlNode myXmlNodeParentNode = myXmlDocument.CreateElement(strParentNodeName);
                myXmlNodeRoot.AppendChild(myXmlNodeParentNode);
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //向一个已经存在的父节点中添加一个子节点,如果不存在该父节点,则向根节点中添加一个父节点(insert)
        public void AddChildNode(string strParentNodeXPath, string strChildNodeName)
        {
            //只处理匹配的第一个Node
            try
            {
                XmlNode myXmlNodeParentNode = myXmlDocument.SelectSingleNode(strParentNodeXPath);
                XmlNode myXmlNodeChildNode = myXmlDocument.CreateElement(strChildNodeName);
                //父节点存在,添加到父节点
                if (myXmlNodeParentNode != null)
                {
                    myXmlNodeParentNode.AppendChild(myXmlNodeChildNode);
                }
                //父节点不存在,添加到根节点
                else
                {
                    XmlNode myXmlNodeRoot = myXmlDocument.DocumentElement;//获得文档的根
                    myXmlNodeRoot.AppendChild(myXmlNodeChildNode);
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //向一个节点添加属性,并赋值(insert)
        public void AddNodeAttributeAndSetValue(string strNodeXPath, string strNodeAttributeName, string strNodeAttributeValue)
        {
            try
            {
                XmlNode myXmlNode = myXmlDocument.SelectSingleNode(strNodeXPath);
                if (myXmlNode != null)
                {
                    XmlAttribute myXmlAttribute = myXmlDocument.CreateAttribute(strNodeAttributeName);
                    myXmlAttribute.Value = strNodeAttributeValue;
                    myXmlNode.Attributes.Append(myXmlAttribute);
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //删除所有节点(匹配节点XPath)(delete)
        public void DelNodeByNodeXPath(string strNodeXPath)
        {
            try
            {
                XmlNodeList myXmlNodeList = myXmlDocument.SelectNodes(strNodeXPath);
                if (myXmlNodeList != null)
                {
                    foreach (XmlNode myXmlNode in myXmlNodeList)
                    {
                        myXmlNode.ParentNode.RemoveChild(myXmlNode);
                    }
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //删除所有节点(匹配节点属性名和属性值)(delete)
        public void DelNodeByNodeAttributeNameAndValue(string strNodeXPath, string strNodeAttributeName, string strNodeAttributeValue)
        {
            try
            {
                XmlNodeList myXmlNodeList = myXmlDocument.SelectNodes(strNodeXPath);
                if (myXmlNodeList != null)
                {
                    foreach (XmlNode myXmlNode in myXmlNodeList)
                    {
                        XmlAttributeCollection myXmlAttributeCollection = myXmlNode.Attributes;
                        for (int i = 0; i < myXmlAttributeCollection.Count; i++)
                        {
                            if (myXmlAttributeCollection.Item(i).Name == strNodeAttributeName && myXmlAttributeCollection.Item(i).Value == strNodeAttributeValue)
                            {
                                myXmlNode.ParentNode.RemoveChild(myXmlNode);
                            }
                        }
                    }
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }

        //删除所有节点的一个属性(匹配节点属性名)(delete)
        public void DelNodeAttributeByNodeAttributeNameAndValue(string strNodeXPath, string strNodeAttributeName)
        {
            try
            {
                XmlNodeList myXmlNodeList = myXmlDocument.SelectNodes(strNodeXPath);
                if (myXmlNodeList != null)
                {
                    foreach (XmlNode myXmlNode in myXmlNodeList)
                    {
                        XmlAttributeCollection myXmlAttributeCollection = myXmlNode.Attributes;
                        for (int i = 0; i < myXmlAttributeCollection.Count; i++)
                        {
                            if (myXmlAttributeCollection.Item(i).Name == strNodeAttributeName)
                            {
                                myXmlNode.Attributes.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            catch (XmlException e)
            {
                throw e;
            }
        }
    }
}

posted on 2007-07-13 14:24  小乔的闺房  阅读(379)  评论(0编辑  收藏  举报

导航