xml - 从一个文件复制 XML 节点并将其附加到另一个 XML 文件
问题描述
请帮助我使用 PowerShell 脚本:
删除 Config-B.xml 中的特定
<Section Name="APILibraries">
节点。<Section Name="APILibraries">
从 Config-A.xml读取特定节点。在 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)
解决方案
推荐阅读
- r - 函数/应用程序在 RStudio 本地工作,但不在 shinyapps.io 上工作
- javascript - 用于 Wordpress 的自定义 CSS 按钮潮汐实时聊天插件
- r - 在 R ggplot 中,关于带有极坐标的“geom_rect”的几个问题
- python - Pandas - 使用 groupby 创建最常用词的元组
- php - 标头位置问题
- powershell - 仅在两次运行脚本时才加载添加类型程序集
- adobe - 在 Adobe Forms 中移动块
- python - 如何不在 Jupyter 笔记本中打印控制台输出
- javascript - 按另一个带日期的数组对对象内的所有数组进行排序
- javascript - 在 React 中更新状态后的函数调用