xml操作

XML如下

<help>
        <email>help@pboedu.com</email>
        <email>help@pboedu.cn</email>
        <tel>010-52926789</tel>
        <tel>010-52926789</tel>
        <mobile>010-52926789</mobile>
        <qq>240100889</qq>
        <qq>82926798</qq>
        <bbslink>bbs.pboedu.com</bbslink>
        <category>
            <item id="01" des="新手上路"></item>
            <item id="02" des="注册登录"></item>
            <item id="03" des="体验试听"></item>
            <item id="04" des="网上书店"></item>
            <item id="05" des="技术支持"></item>
            <item id="06" des="购买付款"></item>
            <item id="07" des="课程学习"></item>
            <item id="08" des="其他问题"></item>
        </category>
    </help>

操作类如下:

/// <summary>
    ///  xml操作辅助类
    /// </summary>

 public class XmlHelper
    {
        protected string strXmlFile;
        protected XmlDocument objXmlDoc = new XmlDocument();

        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="XmlFile">The XML file.</param>
        public XmlHelper(string XmlFile)
        {
            try
            {
                objXmlDoc.Load(XmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            strXmlFile = XmlFile;
        }

        /// <summary>
        ///  获取操作类
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public static XmlHelper getXmlTool(string filename)
        {
            object obj = CacheHelper.Get(filename);
            if (obj == null)
            {
                string filepath = HttpContext.Current.Server.MapPath("~/config/") + filename;
                XmlHelper xt = new XmlHelper(filepath);
                CacheHelper.Add(filename, xt, filepath);
                return xt;
            }
            else
            {
                return (XmlHelper)obj;
            }
        }

        /// <summary>
        ///  存储文档
        /// </summary>
        public void Save()
        {
            try
            {
                objXmlDoc.Save(strXmlFile);
                ClearCache();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///  清除缓存
        /// </summary>
        public void ClearCache()
        {
            string filename = Path.GetFileName(strXmlFile);
            CacheHelper.Remove(filename);
        }

        /// <summary>
        ///  获取多个节点
        /// </summary>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        public XmlNodeList getNodes(string xpath)
        {
            XmlNodeList nodes = objXmlDoc.SelectNodes(xpath);
            return nodes;
        }

        /// <summary>
        ///  获取单个节点
        /// </summary>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        public XmlNode getNode(string xpath)
        {
            return objXmlDoc.SelectSingleNode(xpath);
        }

        ///// <summary>
        /////  获取节点属性
        ///// </summary>
        ///// <param name="XmlPathNode">The XML path node.</param>
        ///// <returns></returns>
        //public CategoryNode GetCategoryNodeibute(string XmlPathNode)
        //{
        //    CategoryNode atr = new CategoryNode();
        //    XmlNode node = objXmlDoc.SelectSingleNode(XmlPathNode);
        //    atr.id = node.Attributes[0].Value;
        //    atr.des = node.Attributes[1].Value;
        //    return atr;
        //}

        ///// <summary>
        /////  获取父节点属性
        ///// </summary>
        ///// <param name="XmlPathNode">The XML path node.</param>
        ///// <returns></returns>
        //public CategoryNode GetParentCategoryNodeibute(string XmlPathNode)
        //{
        //    CategoryNode atr = new CategoryNode();
        //    XmlNode node = objXmlDoc.SelectSingleNode(XmlPathNode);
        //    atr.id = node.ParentNode.Attributes[0].Value;
        //    atr.des = node.ParentNode.Attributes[1].Value;
        //    return atr;
        //}

        ///// <summary>
        ///// 获取爷节点属性
        ///// </summary>
        ///// <param name="XmlPathNode">The XML path node.</param>
        ///// <returns></returns>
        //public CategoryNode GetGrandParentCategoryNodeibute(string XmlPathNode)
        //{
        //    CategoryNode atr = new CategoryNode();
        //    XmlNode node = objXmlDoc.SelectSingleNode(XmlPathNode);
        //    atr.id = node.ParentNode.ParentNode.Attributes[0].Value;
        //    atr.des = node.ParentNode.ParentNode.Attributes[1].Value;
        //    return atr;
        //}

        /// <summary>
        ///  获取节点值
        /// </summary>
        /// <param name="XmlPathNode">The XML path node.</param>
        /// <returns></returns>
        public string GetNodeValue(string XmlPathNode)
        {
            XmlNode node = objXmlDoc.SelectSingleNode(XmlPathNode);
            return node.InnerText;
        }

     
       
        public void ReplaceAttr(string Xpath, string des)
        {
            XmlNode node = objXmlDoc.SelectSingleNode(Xpath);
            node.Attributes[1].Value = des;
        }

        /// <summary>
        ///  删除该节点
        /// </summary>
        /// <param name="Node">The node.</param>
        public void Delete(string Node)
        {
            XmlNode node = objXmlDoc.SelectSingleNode(Node);
            if (node.PreviousSibling != null && node.NextSibling == null)
            {
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                objXmlDoc.SelectSingleNode(mainNode).RemoveChild(node);
            }
        }
        /// <summary>
        ///  将此节点与其前兄弟节点交换
        /// </summary>
        /// <param name="Xpath">The xpath.</param>
        public void PrevMove(string Xpath)
        {
            XmlNode node = objXmlDoc.SelectSingleNode(Xpath);
            XmlNode prevNode = node.PreviousSibling;
            XmlNode parentNode = node.ParentNode;
            if (prevNode.PreviousSibling != null)
                parentNode.InsertBefore(node, prevNode);
        }


        /// <summary>
        ///  插入节点和其子节点
        /// </summary>
        /// <param name="MainNode">The main node.</param>
        /// <param name="ChildNode">The child node.</param>
        /// <param name="Attrib">The attrib.</param>
        /// <param name="AttribContent">Content of the attrib.</param>
        /// <param name="Elements">The elements.</param>
        public void InsertNode(string MainNode, string ChildNode, string Attrib, string AttribContent, params string[] Elements)
        {
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objChildNode.SetAttribute(Attrib, AttribContent);
            objRootNode.AppendChild(objChildNode);
            for (int i = 0; i < Elements.Length; i += 2)
            {
                XmlElement objElement = objXmlDoc.CreateElement(Elements[i]);
                objElement.InnerText = Elements[i + 1];
                objChildNode.AppendChild(objElement);
            }
        }

        /// <summary>
        /// Inserts the content of the node with.
        /// </summary>
        /// <param name="MainNode">The main node.</param>
        /// <param name="ChildNode">The child node.</param>
        /// <param name="content">The content.</param>
        /// <param name="Elements">The elements.</param>
        public void InsertNode(string MainNode, string ChildNode, string content, params string[] Elements)
        {
            //插入一節點和此節點的一子節點。
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objChildNode.InnerText = content;
            for (int i = 0; i < Elements.Length; i += 2)
            {
                objChildNode.SetAttribute(Elements[i], Elements[i + 1]);
            }
            objRootNode.AppendChild(objChildNode);
        }
        public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
        {
            //插入一個節點,帶一屬性。
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(Attrib, AttribContent);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }
        public void InsertElement(string MainNode, string Element, string Content)
        {
            //插入一個節點,不帶屬性。
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }

  public int CountNode(string XmlPathNode)
        {
            try
            {
                int count = objXmlDoc.SelectNodes(XmlPathNode).Count;
                return count;
            }
            catch
            {
                //string xpath = XmlPathNode.Substring(0, XmlPathNode.LastIndexOf("/"));
                //XmlNode pnode = objXmlDoc.SelectSingleNode(xpath);
                return 0;
            }
        }

        /// <summary>
        ///  获取该ID的字符串表示
        /// </summary>
        /// <returns></returns>
        public static string getStrId(int id)
        {
            string strId = id.ToString();
            if ((strId.Length % 2) > 0)
                return "0" + strId;
            return strId;
        }
        #endregion
    }

 

/// <summary>
        ///  获取类型列表
        /// </summary>
        /// <returns></returns>
        public static XmlNodeList GetHelpCatgoryList()
        {
            XmlHelper xtool = XmlHelper.getXmlTool("general.config");
            return xtool.getNodes("/GeneralConfigInfo/help/category/item");
        }

 

 

 

 

posted @ 2008-09-19 16:37  悟〈--觉  阅读(489)  评论(0编辑  收藏  举报