0
我想通过使用PowerShell将现有XML节点从一个文件添加到另一个XML文件。 (从一些Web服务编辑Web.config)。 我尝试了很多方法,但我没有得到它的运行,任何帮助将不胜感激!将xml节点从一个xml文件添加到另一个具有powershell的xml文件到另一个文件中的特定现有节点
我的文件1包含这样一些代码:
<?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),并有到特定的节点。
我的文件2包含这样一些代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
\t <configSections>
\t \t <sectionGroup name="applicationSettings"
\t \t \t type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
\t \t \t <section name="MyServices.Properties.Settings"
\t \t \t \t type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
\t \t </sectionGroup>
\t </configSections>
\t <system.web>
\t \t <compilation debug="true" targetFramework="4.0"/>
\t </system.web>
\t <system.serviceModel>
............ (some more code)
\t \t <diagnostics>
\t \t \t <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
\t \t \t \t maxMessagesToLog="2000"/>
\t \t </diagnostics>
\t \t <services>
\t \t **(HERE I WANT TO ADD MY SERVICES!)**
\t \t \t
\t \t </services>
我还需要修改导入的服务,加入他们一个额外的参数(终点),让它们看起来像这样:(加入地址参数)
<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
感谢您的帮助!