【问题标题】:Adding xml nodes from one xml file into another with powershell into a specific existing node in File 2使用 powershell 将 xml 节点从一个 xml 文件添加到另一个文件 2 中的特定现有节点
【发布时间】: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


    【解决方案1】:

    显然对你来说太晚了,但对其他人来说,简短的版本是在 .NET 的 XML 文档模型中,节点 属于他们的文档,所以你必须...

    • 首先调用 Document.ImportNode 将节点克隆到新文档中
    • 然后在某处实际插入或附加导入的节点

    例如,如果您:

    $Source = [xml]'<root><a><s name="one"/><s name="two"/></a></root>'
    $Dest = [xml]'<root><b><s name="three"/></b></root>'
    foreach($node in $Source.root.a.s) {
        $clone = $Dest.ImportNode($node, $true)
        $null = $Dest.root.b.AppendChild($clode)
    }
    

    那么,您的$Dest.OuterXml 将是:

    <root><b><s name="three" /><s name="one" /><s name="two" /></b></root>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 2016-03-30
      相关资源
      最近更新 更多