【发布时间】:2014-12-22 09:30:00
【问题描述】:
我想使用 PowerShell 将现有 XML 节点从一个文件添加到另一个 XML 文件。 (从一些网络服务编辑 Web.config)。 我尝试了很多方法,但我没有让它运行,任何帮助将不胜感激!
我的 File1 包含如下代码:
<?xml version="1.0"?>
<services>
<service name="MyService.HelloWorld">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloWorld/" contract="MyService.HelloWorld" />
</service>
<service name="MyService.HelloMars">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloMars/" contract="MyService.HelloMars" />
</service>
.........
现在我想将所有服务添加到另一个 xml 文件(Web.config)中,然后添加到特定节点中。
我的 File2 包含如下代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyServices.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
............ (some more code)
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
maxMessagesToLog="2000"/>
</diagnostics>
<services>
**(HERE I WANT TO ADD MY SERVICES!)**
</services>
我还需要修改导入的服务,为它们添加一个额外的参数(Endpoint),使它们看起来像这样:(添加地址参数)
<service name="MyServices.HelloWorld">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewWsHttpBinding" bindingNamespace="http://tempuri.org/HelloWorld/"
address="https://...../HelloWorld.svc" contract="MyServices.HelloWorld"/>
</service>
我尝试了很多在互联网上找到的方法,但都没有奏效。 我尝试至少合并文件的最后一件事是:
$webConfig = [xml](Get-Content C:\...\Web.config)
$webConfigNode = $webConfig.configuration.system.serviceModel.services
$servicesToAdd = [xml](Get-Content C:\...\services.config)
$servicesToAddNode = $servicesToAdd.services
while ($servicesToAddNode.HasChildNodes)
{
$cn = $servicesToAddNode.FirstChild
$cn = $servicesToAddNode.RemoveChild($cn)
$cn = $webConfigNode.OwnerDocument.importnode($cn)
$webConfigNode.AppendChild($cn)
}
但我收到以下错误消息:
It is not possible to run a method for an expression which is null
+ $cn = $webConfig.OwnerDocument.importnode($cn)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
感谢您的帮助!
【问题讨论】:
标签: xml powershell web-config add