在线跪求XML读写操作
web.config
<?xml version= "1.0 "?>
<configuration xmlns= "http://schemas.microsoft.com/.NetConfiguration/v2.0 ">
<appSettings>
<add key= "open " value= "T " />
<add key= "allownewuser " value= "F " > </add>
<add key= "allowupload " value= "F " />
<add key= "allow " value= "F " />
</appSettings>
<connectionStrings/>
<system.web>
<customErrors mode= "Off " />
<globalization culture= "zh-CN " uiCulture= "zh-CHS " />
<compilation debug= "true " defaultLanguage= "c# " />
<httpRuntime maxRequestLength= "2000000 " executionTimeout= "240 "/>
</system.web>
</configuration>
怎样对open,allownewuser等进行读写~在线等 谢谢
------解决方案--------------------ConfigurationSettings.AppSettings[ "open "])
------解决方案--------------------xmlElement.Attributes[ "value "].Value= “F”
------解决方案--------------------string filename=Server.MapPath( "web.config ");
string KeyName;//键名称
XmlDocument xmldoc= new XmlDocument();
XmlNodeList DocdNodeNameArr=xmldoc.DocumentElement.ChildNodes;//文档节点名称数组
foreach(XmlElement DocXmlElement in DocdNodeNameArr)
{
if(DocXmlElement.Name.ToLower()== "appsettings ")//找到名称为 appsettings 的节点
{
XmlNodeList KeyNameArr=DocXmlElement.ChildNodes;//子节点名称数组
if ( KeyNameArr.Count > 0 )
{
foreach(XmlElement xmlElement in KeyNameArr)
{
KeyName=xmlElement.Attributes[ "key "].InnerXml;//键值
switch(KeyName)
{
case "open ":
xmlElement.Attributes[ "value "].Value= "F ";
break;
case "allownewuser ":
xmlElement.Attributes[ "value "].Value= "F ";
break;
//......略
}
}
}
}
}
xmldoc.Save(filename);
Response.Write( " <script> alert( 'OK,信息已保存! ') </script> ");}
------解决方案--------------------protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Server.MapPath( "~ ") + @ "/Web.config ";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName( "add ");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes[ "key "];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "open ")
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes[ "value "];
att.Value = "F ";
break;
}
}