用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

XML文件修改节点属性值(多种方法)

时间:2015-01-29  来源:互联网  作者:佚名
xml 文件内容:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<subtitles>
<info>
<content>最新通告:五一放假七天!请各教员悉知</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字符串的某个节点的属性值,如下:
复制代码 代码如下:
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;

节点userName的值由原来的"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;
来顶一下
返回首页
返回首页
推荐资讯
Asp.net GridView使用大全(分页实现) Asp.net GridView使用大全(分页实现GridView自带的分页功能实现:要实现GrdView分页的功能操作如
基于asp.net下使用jquery实现ajax的解决方法 基于asp.net下使用jquery实现ajax的无论是jquery还是ajax在今天来讨论已经很落后了,网上也有非
相关文章
栏目更新
栏目热门