首页 > 解决方案 > 从一个文件复制 XML 节点并将其附加到另一个 XML 文件

问题描述

请帮助我使用 PowerShell 脚本:

  1. 删除 Config-B.xml 中的特定<Section Name="APILibraries">节点。

  2. <Section Name="APILibraries">从 Config-A.xml读取特定节点。

  3. 在 config-B.xml 中附加第 2 步输出值。

代码块 1 和 2 工作正常。我能够读取值以及从 config-B.xml 中删除节点。但是,在代码块 3 中,PowerShell 脚本的倒数第二行抛出错误,就像它无法导入空节点一样。

这是 Config-A.xml,它是源,Config-B.xml 是目标以及 PowerShell 脚本。

Config-A.xml

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Data>
    <Section Name="APILibraries">
      <Item Name="TIMSS.API.User" Layer="User" Type="Custom" RootNamespace="TIMSS.API.User" FileName="TIMSS.API.User.dll" />
      <Item Name="TIMSS.API.User.Generated" Layer="User" Type="Generated" RootNamespace="TIMSS.API.User" FileName="Personify.API.User.Generated.dll" />
      <Item Name="TIMSS.API.Base" Layer="Base" Type="Custom" RootNamespace="TIMSS.API.Base" FileName="TIMSS.API.Base.dll" />
      <Item Name="TIMSS.API.Generated" Layer="Base" Type="Generated" RootNamespace="TIMSS.API.Generated" FileName="TIMSS.API.Generated.dll" />
      <Item Name="TIMSS.API.Core" Layer="Core" Type="Custom" RootNamespace="TIMSS.API.Core" FileName="TIMSS.API.Core.dll" />
    </Section>
  </Data>
</Configuration>

Config-B.xml

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Data>
    <Section Name="APILibraries">
      <Item Name="TIMSS.API.Base" Layer="Base" Type="Custom" RootNamespace="TIMSS.API.Base" FileName="TIMSS.API.Base.dll" />
      <Item Name="TIMSS.API.Generated" Layer="Base" Type="Generated" RootNamespace="TIMSS.API.Generated" FileName="TIMSS.API.Generated.dll" />
      <Item Name="TIMSS.API.Core" Layer="Core" Type="Custom" RootNamespace="TIMSS.API.Core" FileName="TIMSS.API.Core.dll" />
    </Section>
  </Data>
</Configuration>

PowerShell 脚本:

###Code Block - 1 : Get Source XML <Section Name="APILibraries"> Node Value ###
$CustomWinClientConfigXmlSource = "SourcePath\Config.xml"

[xml]$SourceConfigXml = Get-Content -Path "$CustomWinClientConfigXmlSource" -Raw
$SourceXmlNode = $SourceConfigXml | Select-Xml -XPath "//Section[@Name='APILibraries']"
$SourceXmlOutput = Write-Output "$SourceXmlNode"
$SourceXMLNodeValue = "$SourceXmlOutput"


### Code Block - 2 : Get The Target XML <Section Name="APILibraries"> Node Value And Delete It ##
$WinClientConfigFiles = "Config.xml"
$CustomWinClientConfigXmlTarget = "TargetPath\$WinClientConfigFiles"
$Path = "$CustomWinClientConfigXmlTarget"

[Xml]$servicefactoryconfig = Get-Content -Path $Path -Raw
$old = $servicefactoryconfig.SelectSingleNode("/Configuration/Data/Section[@Name='APILibraries']")
$parent = $old.ParentNode
[void] $parent.RemoveChild($old)


### Code Block - 3 : Append The Target XML <Section Name="APILibraries"> With Source XML Node Value ##
try {
    $newNode = [Xml] @"
$SourceXMLNodeValue
"@
} catch {
    Write-Error -Message 'Ignoring The Error Message' -ErrorAction Ignore
}

[void] $parent.AppendChild($servicefactoryconfig.ImportNode($newNode.DocumentElement, $true))

$servicefactoryconfig.Save($path)

标签: xmlpowershellpowershell-4.0

解决方案


推荐阅读