<?xml version="1.0" encoding="utf-8"?>
<subtitles>
<info>
<content>最新通告:五一放假七天!請(qǐng)各教員悉知</content>
<speed>4</speed>
<color>red</color>
</info>
</subtitles>
C#代碼:
代碼如下:
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"));
另外兩種辦法:
修改xml字符串的某個(gè)節(jié)點(diǎn)的屬性值,如下:
代碼如下:
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;
節(jié)點(diǎn)userName的值由原來(lái)的"ss",變成了"test",然后用doc.OuterXml保存修改后的xml為字符串。
另一種方式:
代碼如下:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<fsdlconfig userName=\"ss\" password=\"134\"/>");
XmlElement att = (XmlElement)doc.FirstChild;
att.SetAttribute("userName","test");
string str = doc.OuterXml;
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com