对XML文档进行增、删、改、查

XML文档

<?xml version="1.0" encoding="utf-8" ?>
<Friends>
  <friend url="#">--友情链接--</friend>
 <friend url="http://www.kingbase.com.cn/home/Default.aspx">北京人大金仓</friend>
 <friend url="http://www.baidu.com">百度</friend>
</Friends>

代码

 /// <summary>
    /// 读取合作伙伴
    /// </summary>
    /// <param name="ToolsName"></param>
    /// <returns></returns>
    public static DataTable Load()
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml");
        DataTable dt = new DataTable();

        dt.Columns.Add("key", typeof(System.String));
        dt.Columns.Add("url", typeof(System.String));
        XmlNodeList menuNodes = xml.SelectNodes("Friends/friend");
        foreach (XmlNode menuNode in menuNodes)
        {
            DataRow dr = dt.NewRow();
            dr["url"] = menuNode.Attributes["url"].Value;
            dr["key"] = menuNode.InnerText;
            dt.Rows.Add(dr);
        }
        return dt;
    }
    /// <summary>
    ///  添加子节点
    /// </summary>
    /// <param name="url"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static void AddFriend(string url,string key)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml");
            XmlNode root = xmlDoc.SelectSingleNode("Friends");//查找<Friends>
            XmlElement xe1 = xmlDoc.CreateElement("friend");//创建一个<friend>节点
            xe1.SetAttribute("url", url);//设置该节点url属性
            xe1.InnerText = key;

            root.AppendChild(xe1);//添加到<bookstore>节点中
            xmlDoc.Save(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml");
        }
        catch (Exception ex)
        {
            LogHelper.WriteLog(ex,null);
        }
    }
    /// <summary>
    /// 删除子节点
    /// </summary>
    /// <param name="key"></param>
    public static void DeleteFriend(string key)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNodeList xnl = xmlDoc.SelectSingleNode(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml").ChildNodes;

            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.InnerText == key)
                {
                    xe.RemoveChild(xn);//删除子节点
                }               
            }
            xmlDoc.Save(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml");
        }
        catch (Exception ex)
        {
            LogHelper.WriteLog(ex,null);
        }
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="key">新key</param>
    /// <param name="value">url值</param>
    /// <param name="oldKey">旧key</param>
    public static void EditorFriend(string key,string value,string oldKey)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Friends").ChildNodes;//获取bookstore节点的所有子节点
        foreach (XmlNode xn in nodeList)//遍历所有子节点
        {
            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
            if (xe.InnerText == oldKey)//如果属性等于oldKey
            {
                xe.SetAttribute("url", value);//则修改该属性为value
                xe.InnerText = key;

                XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点                
                break;
            }
        }
        xmlDoc.Save(HttpContext.Current.Request.PhysicalApplicationPath + "/XML/Friendly.xml");//保存。
    }

posted on 2011-03-11 15:21  MyBeN  阅读(125)  评论(0编辑  收藏  举报

导航