這篇文章將為大家詳細(xì)講解有關(guān)XML文件如何修改節(jié)點(diǎn)屬性值,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
xml 文件內(nèi)容:
代碼如下:
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)的屬性值,如下:
復(fù)制代碼 代碼如下:
XmlDocument doc = new XmlDocument();
doc.LoadXml("
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("
XmlElement att = (XmlElement)doc.FirstChild;
att.SetAttribute("userName","test");
string str = doc.OuterXml;
關(guān)于“XML文件如何修改節(jié)點(diǎn)屬性值”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。