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;
您可能感兴趣的文章:

  • Python获取任意xml节点值的方法
  • python操作xml文件详细介绍
  • 简单介绍使用Python解析并修改XML文档的方法
  • 详解在Python程序中解析并修改XML内容的方法
  • python处理xml文件的方法小结
  • 详解 Python 读写XML文件的实例
  • 对python修改xml文件的节点值方法详解
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。