using System.Xml;
public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("data.xml")); XmlNode node = doc.DocumentElement; XmlNode book = node.SelectSingleNode ( "//book[@id='01']" ); XmlElement bookName = doc.CreateElement ( "name" ); bookName.InnerText = "Xiyouji"; XmlAttribute numbers = doc.CreateAttribute ("numbers"); numbers.Value = "100"; bookName.Attributes.Append ( numbers ); book.AppendChild ( bookName ); doc.Save ( Server.MapPath ( "data.xml" ) ); }}
生成的XML文件内容为
<?xml version="1.0" encoding="gb2312"?><root> <library> <book id="01"> <name numbers="100">Xiyouji</name> </book> <book id="02"> <name numbers="100">Hongloumeng</name> </book> </library></root>