XML File Contents:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<subtitles>
<info>
<content> Latest Notice: 51 Holiday seven days! Please let the teachers know </content>
<speed>4</speed>
<color>red</color>
</info>
</subtitles>
C # code:
Copy Code code as follows:
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 ways:
Modifies the attribute value of a node of an XML string as follows:
Copy Code code 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 from the original "SS" to "Test", and then with Doc. OuterXml saves the modified XML as a string.
Another way:
Copy Code code as follows:
XmlDocument doc = new XmlDocument ();
Doc. Loadxml ("<fsdlconfig username=\" ss\ "password=\" 134\ "/>");
XmlElement att = (xmlelement) doc. FirstChild;
Att. SetAttribute ("UserName", "Test");
String str = doc. OuterXml;