// 打开某文件(假设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.Name.ToLower() == " appsettings " )
{
// 得到该节点的子节点
XmlNodeList nodelist = element.ChildNodes;
if ( nodelist.Count > 0 )
{
// DropDownList1.Items.Clear();
foreach (XmlElement el in nodelist) // 读元素值
{
// DropDownList1.Items.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。
// el.Attributes["value"].Value=this.TextBox2.Text;
}
}
}
}
xmldoc.Save(filename);
在某节点下增加一个元素,并设置值:
if (element.Name.ToLower() == " appsettings " )
{
XmlElement elem = xmldoc.CreateElement( " add " );
element.AppendChild(elem);
elem.InnerText = " ltp " ;
xmldoc.Save(filename);
}
效果:
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did34239