How to update XML nodes with new values?

I have xml inside my App_Data folder. I need to change the values ​​in the nodes of this xml. What I tried is

  XmlDocument xDoc = new XmlDocument(); xDoc.Load(Server.MapPath("~/App_Data/conf.xml.config")); XmlNodeList aNodes = xDoc.SelectNodes("/ConfigInf"); foreach (XmlNode node in aNodes) { XmlNode child1 = node.SelectSingleNode("Node1"); XmlNode child2 = node.SelectSingleNode("Node2"); child1.InnerText = "Value1"; child2.InnerText = "Value2"; } 

I need to rewrite xml with the new values, because when I try again to access the same xml, it must contain the new values. But when I turn to xml, I still get the old (initial) values ​​only when I call it Test.LoadConf(Server.MapPath("./App_Data/conf.xml.config")); . How to write XML with new values ​​or in any other way, for example, create a new xml with new values? (Since I need to access this xml on only one page)

+4
source share
4 answers

call save after editing, you can specify a different name if you do not need to overwrite the original

eg. new file named new.conf.xml.config

 xDoc.Save(Server.MapPath("~/App_Data/new.conf.xml.config")); 

next time you can load the original as usual

 xDoc.Load(Server.MapPath("~/App_Data/conf.xml.config")); 
+6
source

After that you did not save the file

use xDoc.save(Server.MapPath("~/App_Data/conf.xml.config"));

0
source

The nodeValue property can be used to change the value of the node text.

The following code changes the value of the text of the node of the first element: Example:

 xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; 

source: http://www.w3schools.com/DOM/dom_nodes_set.asp

0
source
 node["Node1"].InnerText = "Value1"; node["Node2"].InnerText = "Value2"; 
0
source

Source: https://habr.com/ru/post/1483286/


All Articles