生成xml与获取xml

  private string fileName = "adphoto.xml";


  /// <summary>
        /// 插入XML
        /// </summary>
        /// <param name="id"></param>
        /// <param name="imageName"></param>
        /// <param name="fileName"></param>
        public void AddImg(string id, string imageName, string fileName)
        {
            fileName = "~/xml/" + fileName;
            var sPath = Server.MapPath(fileName);
            XmlDocument doc = new XmlDocument();
            doc.Load(sPath);
            XmlNode root = doc.SelectSingleNode("resultxml");//查找<resultxml>
            XmlElement xe1 = doc.CreateElement("item");
            xe1.SetAttribute("id", id);
            xe1.SetAttribute("thumb", imageName);


            root.AppendChild(xe1);
            doc.Save(sPath); 
        } 




     
  /// <summary>
        /// 更新
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="upimgName"></param>
        /// <param name="fileName"></param>
        public void UpdateImg(string keyName, string upimgName, string fileName)
        {
            fileName = "~/xml/" + fileName;
            var sPath = Server.MapPath(fileName);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(sPath);
            XmlNodeList elemList = xmlDoc.GetElementsByTagName("item");
            foreach (XmlNode list in elemList)
            {
                XmlElement element = list as XmlElement;
                string str = element.GetAttribute("thumb");
                if (str == keyName)
                {
                    element.SetAttribute("thumb", upimgName);
                    element.SetAttribute("id", keyName);
                }
            }
            XmlTextWriter xw = new XmlTextWriter(new StreamWriter(sPath));
            xw.Formatting = Formatting.Indented;
            xmlDoc.WriteTo(xw);
            xw.Close(); 

        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="fileName"></param>
        public void DelImg(string keyName, string fileName)
        {


            // fileName = "photo_list.xml";
            // keyName = "9a.jpg";
            fileName = "~/xml/" + fileName;
            var sPath = Server.MapPath(fileName);
            XmlDocument doc = new XmlDocument();
            doc.Load(sPath);


            XmlNodeList xnl = doc.SelectSingleNode("resultxml").ChildNodes;


            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("thumb") == keyName)
                {


                    doc.SelectSingleNode("resultxml").RemoveChild(xe);
                }
            }
            doc.Save(sPath);


        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值