里面是一个改写XML文件的函数。用来改写WEB。CONFIG的。
当<Configuration带着xmlns时就失败了。如果不带xmlns的话就成功。
有办法不?
函数代码:
public void ModifyXMLSegment(string xmlFileName, string key, string strValue)
{
String TargetDir = "D:\\Projects\\Help2005\\UnitTest";
string XPath = "/configuration xmlns=\"http://schemas.microsoft.com/.NetConfiguration/v2.0\"/connectionStrings/add[@name='?']";
XPath = "/configuration/connectionStrings/add[@name='?']";
XmlDocument domWebConfig = new XmlDocument(); domWebConfig.Load((TargetDir + "\\" + xmlFileName));
XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("Can't found XML segment <add name='" + key + "' connectionString=.../>");
}
addKey.Attributes["connectionString"].InnerText = strValue;
domWebConfig.Save((TargetDir + "\\" + xmlFileName));
}调用的代码是:
ModifyXMLSegment("Web.Config", "SqlServer", "lkjalsdjf;lsadflk09u098080");
要修改的Web.Config内容如下:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="SqlServer" connectionString="Database=eHelpdesk_20061228;Server=Localhost;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
当<Configuration带着xmlns时就失败了。如果不带xmlns的话就成功。
有办法不?
函数代码:
public void ModifyXMLSegment(string xmlFileName, string key, string strValue)
{
String TargetDir = "D:\\Projects\\Help2005\\UnitTest";
string XPath = "/configuration xmlns=\"http://schemas.microsoft.com/.NetConfiguration/v2.0\"/connectionStrings/add[@name='?']";
XPath = "/configuration/connectionStrings/add[@name='?']";
XmlDocument domWebConfig = new XmlDocument(); domWebConfig.Load((TargetDir + "\\" + xmlFileName));
XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("Can't found XML segment <add name='" + key + "' connectionString=.../>");
}
addKey.Attributes["connectionString"].InnerText = strValue;
domWebConfig.Save((TargetDir + "\\" + xmlFileName));
}调用的代码是:
ModifyXMLSegment("Web.Config", "SqlServer", "lkjalsdjf;lsadflk09u098080");
要修改的Web.Config内容如下:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="SqlServer" connectionString="Database=eHelpdesk_20061228;Server=Localhost;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
解决方案 »
- 后台怎么对html控件赋值和取值
- ASP.NET Web Site模板没有了
- Javascript中调用FileUpload控件的Click事件以后,选择的文件路径不能显示到FileUpload的TextBox中
- WEB程序打包时能否把IIS一起打包?
- 加载页面时,页面上有多个textbox,怎么让光标焦点定位在第一个上?
- WEB服务器内存中生成的图片怎样加入网页在IE中显示?
- |zyciis| ASP.NET中 我有A页面要传参到B页面,但这里不允许用URL传参 那有什么办法呢 谢谢
- treeview获取复选框中的值
- asp.net如何实现窗体对话确认框
- 文件上传时数组循环插入的问题
- 请问以下这个web.config的修改函数错在哪?搞不懂。
- 求梅花雪老大的树型目录控件mztreeview10.rar版本
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//删除appSettings节点中的元素
appSection.Settings.Remove("addkey1");
//修改appSettings节点中的元素
appSection.Settings["addkey2"].Value = "Modify key2's value";
config.Save();