The XML file modifies the node property value of in several ways

  • 2020-06-03 06:19:27
  • OfStack

xml File content:
 
<?xml version="1.0" encoding="utf-8"?> 
<subtitles> 
<info> 
<content> Latest Announcements: 51 Have a holiday 7 Day! Please be informed by teachers </content> 
<speed>4</speed> 
<color>red</color> 
</info> 
</subtitles> 

C # code:
 
XmlDocument xml = new XmlDocument(); 
xml.Load(context.Server.MapPath("~/js/XMLFile.xml")); 
XmlNode xn = xml.DocumentElement; 
foreach (XmlNode node in xn.ChildNodes) 
{ 
if (node.Name == "info") 
{ 
node["content"].InnerText = content; 
node["speed"].InnerText = speed; 
node["color"].InnerText = color; 
} 
} 
xml.Save(context.Server.MapPath("~/js/XMLFile.xml")); 

Two other approaches:
Modify the property value of a node in the xml string as follows:
 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<fsdlconfig userName=\"ss\" password=\"134\"/>"); 
XmlAttribute att =(XmlAttribute)doc.SelectSingleNode("/fsdlconfig/@userName"); 
Console.WriteLine(att.Value); 
att.Value = "test"; 
string str = doc.OuterXml; 

The value of the node userName changed from "ss" to "test", and then saved the modified xml as a string with doc.OuterXml.
Another way:
 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<fsdlconfig userName=\"ss\" password=\"134\"/>"); 
XmlElement att = (XmlElement)doc.FirstChild; 
att.SetAttribute("userName","test"); 
string str = doc.OuterXml; 

Related articles: