生活随笔
收集整理的這篇文章主要介紹了
C# XML 添加,修改,删除Xml节点
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?????private?void?AddXml(string?image,?string?title)??????{?????????XmlDocument?xmlDoc?=?new?XmlDocument();?????????xmlDoc.Load(Server.MapPath("../flash/dati.xml"));?????????XmlNode?root?=?xmlDoc.SelectSingleNode("images");?????????XmlElement?xe1?=?xmlDoc.CreateElement("thumb");?????????xe1.SetAttribute("displayNum",?"6");?????????xe1.SetAttribute("separation",?"5");?????????XmlElement?xesub1?=?xmlDoc.CreateElement("image");?????????xesub1.InnerText?=?image;?????????xe1.AppendChild(xesub1);?????????XmlElement?xesub2?=?xmlDoc.CreateElement("description");?????????xesub2.InnerText?=?title;?????????xe1.AppendChild(xesub2);?????????root.AppendChild(xe1);?????????xmlDoc.Save(Server.MapPath("../flash/dati.xml"));?????}?????????????private?void?DelXml(string?image)?????{?????????XmlDocument?xmlDoc?=?new?XmlDocument();?????????xmlDoc.Load(Server.MapPath("../flash/dati.xml"));???????????XmlNodeList?xnl?=?xmlDoc.SelectSingleNode("images").ChildNodes;???????????????????foreach?(XmlNode?xn?in?xnl)?????????{?????????????XmlElement?xe?=?(XmlElement)xn;?????????????if?(xe.InnerText.IndexOf(image,0)?>=?0)?????????????{?????????????????xn.ParentNode.RemoveChild(xn);?????????????????????????????}?????????}?????????xmlDoc.Save(Server.MapPath("../flash/dati.xml"));?????}??????????foreach(XmlNode?xn?in?nodeList)????{?????XmlElement?xe=(XmlElement)xn;?????if(xe.GetAttribute("genre")=="")?????{??????xe.SetAttribute("genre",newStr);??????XmlNodeList?nls=xe.ChildNodes;??????foreach(XmlNode?xn1?in?nls)??????{???????XmlElement?xe2=(XmlElement)xn1;???????if(xe2.Name=="author")???????{????????xe2.InnerText=newText;????????break;???????}??????}??????break;?????}?? //添加xml節(jié)點(diǎn)private void AddXml(string image, string title) {XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("../flash/dati.xml"));XmlNode root = xmlDoc.SelectSingleNode("images");//查找<images>XmlElement xe1 = xmlDoc.CreateElement("thumb");//創(chuàng)建一個(gè)<thumb>節(jié)點(diǎn)xe1.SetAttribute("displayNum", "6");//設(shè)置該節(jié)點(diǎn)displayNum屬性xe1.SetAttribute("separation", "5");//設(shè)置該節(jié)點(diǎn)separation屬性XmlElement xesub1 = xmlDoc.CreateElement("image");xesub1.InnerText = image;//設(shè)置文本節(jié)點(diǎn)xe1.AppendChild(xesub1);//添加到thumb節(jié)點(diǎn)中XmlElement xesub2 = xmlDoc.CreateElement("description");xesub2.InnerText = title;xe1.AppendChild(xesub2);root.AppendChild(xe1);//添加到<images>節(jié)點(diǎn)中xmlDoc.Save(Server.MapPath("../flash/dati.xml"));}//刪除節(jié)點(diǎn)內(nèi)容private void DelXml(string image){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("../flash/dati.xml"));XmlNodeList xnl = xmlDoc.SelectSingleNode("images").ChildNodes; //查找節(jié)點(diǎn)foreach (XmlNode xn in xnl){XmlElement xe = (XmlElement)xn;if (xe.InnerText.IndexOf(image,0) >= 0){xn.ParentNode.RemoveChild(xn);// xn.RemoveAll();}}xmlDoc.Save(Server.MapPath("../flash/dati.xml"));}//修改foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點(diǎn){XmlElement xe=(XmlElement)xn;//將子節(jié)點(diǎn)類型轉(zhuǎn)換為XmlElement類型if(xe.GetAttribute("genre")=="")// 判讀條件{xe.SetAttribute("genre",newStr);//則修改該屬性為newstr XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)foreach(XmlNode xn1 in nls)//遍歷{XmlElement xe2=(XmlElement)xn1;//轉(zhuǎn)換類型if(xe2.Name=="author")//如果找到 //判讀條件{xe2.InnerText=newText;//則修改 break;//找到退出來(lái)就可以了}}break;}
總結(jié)
以上是生活随笔為你收集整理的C# XML 添加,修改,删除Xml节点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。