xml与.net

来源:百度知道 编辑:UC知道 时间:2024/07/11 01:48:51
<?xml version="1.0" encoding="UTF-8"?>
<graph>
<serie id="1" icolor="0xFF0909" ocolor="0x880000" lcolor="0xFF0909" name="电影" value="30" description="电影"></serie>
<serie id="2" icolor="0xFDE155" ocolor="0xDD8800" lcolor="0xFF9F35" name="Books" value="55" description="Books increasing"></serie>

</graph>
.net如何用编程的方式改变value的值?

string xmlFile = Server.MapPath("XMLFile1.xml");
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(xmlFile);

System.Xml.XmlNodeList list = xmlDoc.GetElementsByTagName("serie");
foreach(System.Xml.XmlNode node in list)
{
node.Attributes["value"].Value = "3333";
}

xmlDoc.Save(xmlFile);

这个你用XmlDocument

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("xxxx.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("graph").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
if (if(xe.Name=="serie")
{