//打开某文件(假设web
config 在根目录中) string filename=Server
MapPath("/") + @"WebApplication1\web
config"; XmlDocument xmldoc= new XmlDocument(); xmldoc
Load(filename); //得到顶层节点列表 XmlNodeList topM=xmldoc
DocumentElement
ChildNodes; foreach(XmlElement element in topM) { if(element
ToLower()=="appsettings") { //得到该节点的子节点 XmlNodeList nodelist=element
ChildNodes; if ( nodelist
Count >0 ) { //DropDownList1
Clear(); foreach(XmlElement el in nodelist)//读元素值 { //DropDownList1
Add(el
Attributes["key"]
InnerXml); //this
TextBox2
Text=el
Attributes["key"]
InnerText; this
TextBox2
Text=el
Attributes["key"]
Value; this
Label1
Text=el
Attributes["value"]
Value; //同样在这里可以修改元素值,在后面save
Attributes["value"]
Value=this
TextBox2
Text; } } } } xmldoc
Save(filename); } 效果: //添加空元素: XmlNode node=do