XML文档内容如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
<first >
<second />
<second />
<second />
<second />
</first>
<first >
<second />
<second />
</first>
</root>
给XML指定的标签内添加标签
string xmlpath = "/xml/role.config";
XDocument doc = XDocument.Load(Server.MapPath(xmlpath));
XElement root = doc.Element("root");
XElement first = new XElement("first");
first.Add(new XAttribute("id", id), new XAttribute("name", name));
root.Add(first);
doc.Save(Server.MapPath(xmlpath));
修改指定的标签
string xmlpath = "/xml/role.config";
XDocument doc = XDocument.Load(Server.MapPath(xmlpath));
XElement x = doc.Descendants("first").FirstOrDefault(t => t.Attribute("id").Value == id.ToString());
x.Attribute("name").Value = name;
doc.Save(Server.MapPath(xmlpath));