注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

.net 操作xml类  

2011-03-07 14:40:23|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
public class Cls_XML
{
    #region 创建xml文件
    /// <summary>
    /// 创建xml文件
    /// </summary>
    /// <param name="filepath"></param>
    public void CreateXmlFile(string filepath, string filetext)
    {
        XmlDocument xmldoc = new XmlDocument(); //创建空的XML文档 
        //xmldoc.LoadXml("<?xml version='1.0' encoding='gb2312'?>" +
        // "<bookstore>" +
        // "<book genre='fantasy' ISBN='2-3631-4'>" +
        // "<title>Oberon's Legacy</title>" +
        // "<author>Corets, Eva</author>" +
        // "<price>5.95</price>" +
        // "</book>" +
        // "</bookstore>");
        xmldoc.LoadXml(filetext);
        xmldoc.Save(filepath); //保存 
    }
    #endregion
    #region 为父节点添加 子节点(子节点可以带属性)
    /// <summary>
    /// 为父节点添加 子节点(ArrayList 存放 Hashtable  第一个是节点名称,其他为节点属性)
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="PatentNod"></param>
    /// <param name="arrL">ArrayList 存放 Hashtable 第一个是节点名称,其他为节点属性,存放</param>
    public void AddXmlNode(string filepath, string ParentNode, ArrayList arrL)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNode root = xmlDoc.SelectSingleNode(ParentNode);//查找父节点
        for (int i = 0; i < arrL.Count; i++)
        {
            Hashtable ht = (Hashtable)arrL[i];
            IDictionaryEnumerator de = ht.GetEnumerator();
            XmlElement xes = xmlDoc.CreateElement(de.Value.ToString());
            de.MoveNext();
            while (de.MoveNext())
            {
                xes.SetAttribute(de.Key.ToString(), de.Value.ToString());
            }
        }
        xmlDoc.Save(filepath);
    }
    #endregion
    #region 为父节点添加 子节点(子节点无属性)  不检查是否存在相同值的节点
    /// <summary>
    /// 为父节点添加 子节点(子节点无属性) 不检查是否存在相同值的节点
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="ParentNode">最大父节点</param>
    /// <param name="NextNode">父节点的子节点</param>        /// 
    /// <param name="ht_Nod">
    /// 子节点的集合 哈希表 (key =节点名称,value =节点值)
    /// </param>
    public void AddXmlNode(string filepath, string ParentNode, string NextNode, Hashtable ht_Nod)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNode root = xmlDoc.SelectSingleNode(ParentNode);//查找父节点
        XmlElement xesub = xmlDoc.CreateElement(NextNode);
        // 作哈希表循环
        IDictionaryEnumerator de = ht_Nod.GetEnumerator();
        while (de.MoveNext())
        {
            XmlElement xesub1 = xmlDoc.CreateElement(de.Key.ToString());
            xesub1.InnerText = de.Value.ToString();
            xesub.AppendChild(xesub1);
        }
        root.AppendChild(xesub);
        xmlDoc.Save(filepath);
    }
    #endregion
    #region 节点添加 子节点(子节点无属性)  检查是否存在相同值的节点
    /// <summary> 
    /// 为父节点添加 子节点(子节点无属性) 检查是否存在相同值的节点
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="ParentNode">最大父节点</param>
    /// <param name="NextNode">父节点的子节点</param>         
    /// <param name="key_name">检查子节点name</param>
    /// <param name="key_value">检查子节点 InnerText</param> 
    /// <param name="ht_Nod">
    /// 子节点的集合 哈希表 (key =节点名称,value =节点值)
    /// </param> 
    public bool AddXmlNode(string filepath, string ParentNode, string NextNode, string key_name, string key_value, Hashtable ht_Nod)
    {
        try
        {
            bool b = false;
            int y = 0;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode root = xmlDoc.SelectSingleNode(ParentNode);//查找父节点   
            XmlNodeList root_xnl = root.ChildNodes;
            foreach (XmlNode xn in root_xnl)
            {
                y = 0;
                XmlNodeList xnl_xn = xn.ChildNodes;
                foreach (XmlNode xn_xn in xnl_xn)
                {
                    if (xn_xn.Name == key_name)
                    {
                        y = 1;
                        if (xn_xn.InnerText == key_value)
                        {
                            b = true;
                            break;
                        }
                    }
                    if (y == 1)
                    {
                        break;
                    }
                }
                if (b)
                {
                    break;
                }
            }
            if (!b)
            {
                XmlElement xesub = xmlDoc.CreateElement(NextNode);
                // 作哈希表循环
                IDictionaryEnumerator de = ht_Nod.GetEnumerator();
                while (de.MoveNext())
                {
                    XmlElement xesub1 = xmlDoc.CreateElement(de.Key.ToString());
                    xesub1.InnerText = de.Value.ToString();
                    xesub.AppendChild(xesub1);
                }
                root.AppendChild(xesub);
                xmlDoc.Save(filepath);
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    #endregion
    #region 节点添加 子节点(子节点无属性)  检查是否存在相同值的节点(多个条件)
    /// 为父节点添加 子节点(子节点无属性) 检查是否存在相同值的节点(多个条件)
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="ParentNode">最大父节点</param>
    /// <param name="NextNode">父节点的子节点</param>           
    /// <param name="ht_strwhere">key=检查子节点name, value=检查子节点 InnerText</param>
    /// <param name="ht_Nod">
    /// 子节点的集合 哈希表 (key =节点名称,value =节点值)
    /// </param> 
    public bool AddXmlNode(string filepath, string ParentNode, string NextNode,Hashtable ht_strwhere,Hashtable ht_Nod)
    {
        try
        {
            bool b = false; 
            int y = 0;
            int wherecount = 0;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode root = xmlDoc.SelectSingleNode(ParentNode);//查找父节点   
            XmlNodeList root_xnl = root.ChildNodes;
            foreach (XmlNode xn in root_xnl)
            {
                y = 0;
                wherecount = 0;
                XmlNodeList xnl_xn = xn.ChildNodes;
                foreach (XmlNode xn_xn in xnl_xn)
                {
                    IDictionaryEnumerator de_where = ht_strwhere.GetEnumerator();
                    while (de_where.MoveNext())
                    {
                        if (xn_xn.Name == de_where.Key.ToString())
                        {
                            if (xn_xn.InnerText == de_where.Value.ToString())
                            {
                                wherecount += 1;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (wherecount >= ht_strwhere.Count)
                    {
                        b = true;
                        break;
                    }
                }
                if (b)
                {
                    break;
                }
            }
            if (!b)
            {
                XmlElement xesub = xmlDoc.CreateElement(NextNode);
                // 作哈希表循环
                IDictionaryEnumerator de = ht_Nod.GetEnumerator();
                while (de.MoveNext())
                {
                    XmlElement xesub1 = xmlDoc.CreateElement(de.Key.ToString());
                    xesub1.InnerText = de.Value.ToString();
                    xesub.AppendChild(xesub1);
                }
                root.AppendChild(xesub);
                xmlDoc.Save(filepath);
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public bool AddXmlNode(string filepath, string ParentNode, string NextNode, Hashtable ht_strwhere,ArrayList arr)
    {
        try
        {
            bool b = false;
            int y = 0;
            int wherecount = 0;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode root = xmlDoc.SelectSingleNode(ParentNode);//查找父节点   
            XmlNodeList root_xnl = root.ChildNodes;
            foreach (XmlNode xn in root_xnl)
            {
                y = 0;
                wherecount = 0;
                XmlNodeList xnl_xn = xn.ChildNodes;
                foreach (XmlNode xn_xn in xnl_xn)
                {
                    IDictionaryEnumerator de_where = ht_strwhere.GetEnumerator();
                    while (de_where.MoveNext())
                    {
                        if (xn_xn.Name == de_where.Key.ToString())
                        {
                            if (xn_xn.InnerText == de_where.Value.ToString())
                            {
                                wherecount += 1;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (wherecount >= ht_strwhere.Count)
                    {
                        b = true;
                        break;
                    }
                }
                if (b)
                {
                    break;
                }
            }
            if (!b)
            {
                XmlElement xesub = xmlDoc.CreateElement(NextNode);
                for (int i = 0; i < arr.Count; i++) 
                {
                    string[] str = new string[2];
                    str = (string[])arr[i];
                    XmlElement xesub1 = xmlDoc.CreateElement(str[0]);
                    xesub1.InnerText = str[1];
                    xesub.AppendChild(xesub1);
                } 
                root.AppendChild(xesub);
                xmlDoc.Save(filepath);
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    #endregion
    #region 删除节点 (根据子节点的name 和 InnerText) 只有一个条件
    /// <summary>
    /// 删除节点 (根据子节点的name 和 InnerText) 只有一个条件
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="ParentNode">最大父节点</param>
    /// <param name="key_name"></param>
    /// <param name="key_value"></param>
    /// <param name="type">类型 0: 修改所有的记录都检查, 1: 一条记录 ,2:检查2条记录 ,3 依次类推</param>
    /// <returns></returns>
    public int DelXmlNode(string filepath, string ParentNode, string key_name, string key_value, int type)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(ParentNode).ChildNodes;
        int i = 0;
        int y = 0;
        int x = 0;
        foreach (XmlNode xn in xnl)
        {
            y = 0;
            x = 0;
            XmlNodeList xnl_xn = xn.ChildNodes;
            foreach (XmlNode xn_xn in xnl_xn)
            {
                if (xn_xn.Name == key_name)
                {
                    x = 1;
                    if (xn_xn.InnerText == key_value)
                    {
                        y = 1;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                if (x == 1)
                {
                    break;
                }
            }
            if (y == 1)
            {
                xn.RemoveAll();
                i++;
                y = 0;
            }

            if (type > 0)
            {
                if (i >= type)
                {
                    break;
                }
            }
        }
        xmlDoc.Save(filepath);
        return i;
    }
    #endregion
    #region 删除节点 (根据子节点的name 和 InnerText) 多条件判断
    /// <summary>
    /// 删除节点 (根据子节点的name 和 InnerText) 多条件判断
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="ParentNode">最大父节点</param>   
    /// <param name="arr"> ArraryList条件, 里面存放 Hashtable  key=子节点Name Value=子节点innerText</param>
    /// <returns></returns>
    public int DelXmlNode(string filepath, string ParentNode,ArrayList arr)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(ParentNode).ChildNodes;
        int i = 0;
        int y = 0;
        int x = 0;
        int okcount = 0;
        for (int a = xnl.Count - 1; a >= 0; a--)
        {
            // xnl.Item(a).ParentNode.RemoveChild(xnl.Item(a));
            y = 0;
            for (int iarr = 0; iarr < arr.Count; iarr++)
            {
                okcount = 0;
                Hashtable ha = (Hashtable)arr[iarr];
                IDictionaryEnumerator de_where = ha.GetEnumerator();
                while (de_where.MoveNext())
                {
                    XmlNodeList xnl_xn = xnl.Item(a).ChildNodes;
                    foreach (XmlNode xn_xn in xnl_xn)
                    {
                        if (xn_xn.Name == de_where.Key.ToString())
                        {
                            x = 1;
                            if (xn_xn.InnerText.Trim() == de_where.Value.ToString().Trim())
                            {
                                okcount += 1;
                            }
                            break;
                        }
                    } 
                }
                if (okcount == ha.Count)
                {
                    y = 1;
                    break;
                }
                else
                {
                    y = 0;
                }
            }
            if (y == 1)
            {
                xnl.Item(a).ParentNode.RemoveChild(xnl.Item(a));
                i++;
                y = 0; 
            }
        }
        xmlDoc.Save(filepath);
        return i;
    }
    #endregion
    #region 修改节点 (依据节点的属性)
    /// <summary>
    /// 修改节点 (依据节点的属性)
    /// </summary>
    /// <param name="filepath"></param>  
    /// <param name="ParentNode">父节点</param>
    /// <param name="key_AttributeName">key节点属性名字</param>
    /// <param name="oldStr"></param>
    /// <param name="Edit_AttributeName">修改属性名字</param>
    /// <param name="newStr"></param>
    /// <returns></returns>
    public int EditXmlNode_Attribute(string filepath, string ParentNode, string key_AttributeName, string oldStr, string Edit_AttributeName, string newStr)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(ParentNode).ChildNodes;
        int i = 0;
        foreach (XmlNode xn in xnl)
        {
            XmlElement xe = (XmlElement)xn;
            if (xe.GetAttribute(key_AttributeName) == oldStr)
            {
                i++;
                xe.SetAttribute(Edit_AttributeName, newStr);
            }
        }
        xmlDoc.Save(filepath);
        return i;
    }
    #endregion
    #region 修改节点 (依据子节点的值) 修改某个字段
    /// <summary>
    /// 修改节点 (依据子节点的值) 修改某个字段
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="parentnod">父节点</param>
    /// <param name="key_node">已知子节点的name</param>
    /// <param name="key_value">已知子节点的value</param>
    /// <param name="edit_node">要修改的子节点</param>
    /// <param name="edit_value">要修改的子节点的值</param>
    /// <param name="type">类型 0: 修改所有的记录都检查, 1: 一条记录 ,2:检查2条记录 ,3 依次类推</param>
    /// <returns></returns>
    public int EditXmlNode(string filepath, string parentnod, string key_node, string key_value, string edit_node, string edit_value, int type)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(parentnod).ChildNodes;
        int i = 0;
        int y = 0;
        int x = 0;
        foreach (XmlNode xn in xnl)
        {
            x = 0;
            y = 0;
            XmlElement xe = (XmlElement)xn;
            XmlNodeList xnl_xe = xe.ChildNodes;
            foreach (XmlNode xn_xe in xnl_xe)
            {
                XmlElement xe_xe = (XmlElement)xn_xe;  //修改子节点的值
                if (y == 1 && x == 1)
                {
                    if (xe_xe.Name == edit_node)
                    {
                        xe_xe.InnerText = edit_value;
                        i++;
                        y = 0;
                        x = 0;
                        break;
                    }
                }
                else
                {
                    if (xe_xe.Name == key_node)   // 找到含有的key的子节点
                    {
                        if (xe_xe.InnerText == key_value)
                        {
                            y = 1;
                            x = 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            if (type > 0)
            {
                if (i >= type)
                {
                    break;
                }
            }
        }
        xmlDoc.Save(filepath);
        return i;
    }
    #endregion
    #region 修改节点 (依据子节点的值) 修改多个子节点
    /// <summary>
    /// 修改节点 (依据子节点的值) 修改多个子节点
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="parentnod">父节点</param>
    /// <param name="key_node">已知子节点的name</param>
    /// <param name="key_value">已知子节点的value</param>
    /// <param name="ha"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public int EditXmlNode(string filepath, string parentnod, string key_node, string key_value, Hashtable ha, int type)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(parentnod).ChildNodes;
        int i = 0;
        int y = 0;
        int x = 0;
        foreach (XmlNode xn in xnl)
        {
            x = 0;
            y = 0;
            XmlElement xe = (XmlElement)xn;
            XmlNodeList xnl_xe = xe.ChildNodes;
            foreach (XmlNode xn_xe in xnl_xe)
            {
                y = 0;
                x = 0;
                if (xn_xe.Name == key_node)   // 找到含有的key的子节点
                {
                    if (xn_xe.InnerText == key_value)
                    {
                        y = 1;
                        x = 1;
                    }
                    else
                    {
                        break;
                    }
                }
                if (y == 1 && x == 1)
                {
                    break;
                }
            }
            if (y == 1 && x == 1)
            {
                i++;
                IDictionaryEnumerator de = ha.GetEnumerator();
                while (de.MoveNext())
                {
                    foreach (XmlNode xn_xe in xnl_xe)
                    {
                        if (de.Key.ToString() == xn_xe.Name)
                        {
                            xn_xe.InnerText = de.Value.ToString();
                            break;
                        }
                    }
                }
            }
            if (type > 0)
            {
                if (i >= type)
                {
                    break;
                }
            }
        }
        xmlDoc.Save(filepath);
        return i;
    }
    #endregion
    #region  检查节点是否包含某个子节点 (依据子节点的值)
    /// <summary>
    /// 检查节点是否包含某个子节点 (依据子节点的值)
    /// </summary>
    /// <param name="filepath">文件名</param>
    /// <param name="parentnod">父节点</param>
    /// <param name="key_node">包含子节点name</param>
    /// <param name="key_value">子节点的value</param>
    /// <returns></returns>
    public bool CheckXmlNode(string filepath, string parentnod, string key_node, string key_value)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList xnl = xmlDoc.SelectSingleNode(parentnod).ChildNodes;
        bool y = false;
        foreach (XmlNode xn in xnl)
        {
            XmlNodeList xnl_xn = xn.ChildNodes;
            foreach (XmlNode xn_xn in xnl_xn)
            {
                if (xn_xn.Name == key_node)
                {   //当检查到当前的节点包含了name相同的子节点时,如果值不对,就跳入下一个节点查找
                    if (xn_xn.InnerText == key_value)
                    {
                        y = true;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (y)
            {
                break;
            }
        }
        return y;
    }
    #endregion
    #region 提取xml数据
    /// <summary>
    /// 
    /// </summary>
    /// <param name="filepath">文件路径</param>
    /// <param name="ParentNode">最大父节点</param>
    /// <param name="ha">要遍历的子节点name</param> 
    /// <param name="dt">对应保存的datatble</param>
    /// <returns>返回保存的dt</returns>
    public DataTable ReadXmlFile(string filepath, string ParentNode, Hashtable ha, DataTable dt)
    {
        int i = 0;
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(filepath);
        XmlNodeList xnl = xmldoc.SelectSingleNode(ParentNode).ChildNodes;
        bool b = false; 
        foreach (XmlNode xn in xnl)
        {
            DataRow dr = dt.NewRow();
            XmlNodeList xnl_xn = xn.ChildNodes;
            foreach (XmlNode xn_xn in xnl_xn)
            {
                IDictionaryEnumerator ed = ha.GetEnumerator();
                while (ed.MoveNext())
                {
                    if (xn_xn.Name == ed.Value.ToString())
                    {
                        dr[ed.Key.ToString()] = xn_xn.InnerText;
                        b = true;
                        break;
                    }
                    else 
                    {
                        b = false;
                    }
                } 
            } 
            dt.Rows.Add(dr);         
        } 
        return dt;
    }
    #endregion
}
 
又添加了几个方法
  评论这张
 
阅读(554)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017