asp.Net 读取,删除(包括父节点)配置文件

<?xml version="1.0" encoding="gb2312"?>
<root>
  <comment>
    <url>http://image.meimumeijia.com/product_comment/2013/08/327956_201308161103132272_small.jpg</url>
    <desc>咚咚咚咚,测试测试!</desc>
  </comment>
  <comment>
    <url>http://image.meimumeijia.com/product_comment/2013/08/327956_201sdfsdf272_small.jpg</url>
    <desc>咚咚咚咚,测试测试sdfs!</desc>
  </comment>
</root>

 /// <summary>

        /// 创建、添加 xml节点
        /// </summary>
        /// <param name="url">图片链接</param>
        /// <param name="desc">描述</param>
        public void CreateXmlNode(string url,string desc)
        {        
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("/") + "Product/comment.xml");

            XmlNode root = xmlDoc.SelectSingleNode("root");
            XmlElement xe1 = xmlDoc.CreateElement("comment");

            XmlElement xesub1 = xmlDoc.CreateElement("url");
            xesub1.InnerText =url;
            xe1.AppendChild(xesub1);

            XmlElement xesub2 = xmlDoc.CreateElement("desc");
            xesub2.InnerText =desc;
            xe1.AppendChild(xesub2);

            root.AppendChild(xe1);
            xmlDoc.Save(System.Web.HttpContext.Current.Server.MapPath("/") + "Product/comment.xml");

        }
        /// <summary>
        /// 删除xml文件第一个子节点
        /// </summary>
        public void DeleteXmlNode()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("/") + "Product/comment.xml");
            XmlNodeList xnl = xmlDoc.SelectSingleNode("root").ChildNodes;
            if (xnl.Count < 5)
            {
                return;
            }
            foreach (XmlNode xn in xnl)
            {
                if (xnl.Item(0) == xn)
                {
                    xn.ParentNode.RemoveChild(xn);//删除节点(包括副节点本身)
                    break;
                }
            }

            xmlDoc.Save(System.Web.HttpContext.Current.Server.MapPath("/") + "Product/comment.xml");
        }

你可能感兴趣的:(asp.net,XML添加节点,XML删除父节点)