C#: delete an element from xml -


i writing , reading xml file c# winfroms level. additionally want have function delete element given content. xml format:

<libraryimages>     <imagelink>*link1*</imagelink>     <imagelink>*link2*</imagelink> </libraryimages> 

function body:

system.xml.linq.xdocument xdoc = system.xml.linq.xdocument.load("xmldata.xml");             xdoc.root.elements("imagelink").select(el => el).where(el => el.value == pathtoremove).tolist().foreach(el => el.remove()); 

as 'pathtoremove' parameter pass link1 example. thing - doesn't remove element xml - after restart application content of library previously, if hadn't removed item. why doesn't work? i've browsed through many of stackoverflow questions, though i've found nothing.

you should update xml file after in-memory manipulations:

// read file disc , build in-memory representation of xml var xdoc = xdocument.load("xmldata.xml");  // modify in-memory representation xdoc.root.elements("imagelink").where(el => el.value == pathtoremove).remove();  // save modified representation disck xdoc.save("xmldata.xml"); 

Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -