我正在使用powershell的XML功能来修改.config文件.除非我提供完整的路径名,否则调用XMLDocument.Save没有任何效果.
# Open the xml file
$config = [xml](get-content web.config)
#
# modify the XML
$config.SelectNodes("./configuration/connectionStrings/add[@name='LocalSqlServer']") | % { $connNode = $_ }
$connNode.connectionString = $connNode.connectionString -replace '^(.*)Server=[^;]+(.*)$', '$1Server=192.168.5.2$2'
#
#
# Now I save it again
#
# This doesn't save it!
$config.Save("web.config");
# However, this works
$config.Save("{0}\\web.config" -f (get-location));
Run Code Online (Sandbox Code Playgroud)
为什么$ config.Save("web.config")不起作用?
除了本地目录以外,我最终还是将它保存在其他地方吗?
我想将以下内容添加到web.config的httpHandler部分:
<add name="Telerik_Web_UI_DialogHandler_aspx" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" />
Run Code Online (Sandbox Code Playgroud)
目前我正在创建一个节点并设置如下属性:
$xmlDoc = (Get-Content $webConfig) -as [Xml]
$root = $xmlDoc.get_DocumentElement();
$handler1 = $xmlDoc.CreateNode('element',"add",'')
$handler1.SetAttribute('name','Telerik_Web_UI_DialogHandler_aspx')
$handler1.SetAttribute('verb','*')
$handler1.SetAttribute('preCondition','integratedMode')
$handler1.SetAttribute('path','Telerik.Web.UI.DialogHandler.aspx')
$handler1.SetAttribute('type','Telerik.Web.UI.DialogHandler')
$root.'system.webServer'.handlers.AppendChild($handler1);
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将字符串转换为节点并附加它?或者只是在孩子们的末尾添加字符串?