一、读取XML信息
using System.Xml;
/// <summary>
/// 读取xml节点属性值
/// setxml(xml路径,节点路径,节点属性名)
/// </summary>
public static string getxml(string xml, string node, string name)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xml);
var root = xmlDoc.DocumentElement;//取到根结点
XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode(node);
return node1.GetAttribute(name).ToString();
}
二、修改XML信息
using System.Xml;
/// <summary>
/// 修改xml节点属性值
/// getxml(xml路径,节点路径,节点属性名,值)
/// </summary>
public static void setxml(string xml, string node, string name, string value)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xml);
var root = xmlDoc.DocumentElement;//取到根结点
XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode(node);
node1.SetAttribute(name, value);
xmlDoc.Save(xml);
}
本文介绍了如何使用C#的System.Xml命名空间,通过XmlDocument类实现XML文件的读取,获取指定节点的属性值,以及修改节点属性并保存回原文件。
402

被折叠的 条评论
为什么被折叠?



