【发布时间】:2016-03-03 04:44:10
【问题描述】:
我正在运行我的 PowerShell 脚本,基本上是从几个较小的 XML 文件创建一个大的 XML 文件。
脚本打开 Template.txt:
<?xml version="1.0" encoding="utf-8"?>
<Model>
<LobSystems>
<LobSystem>
<LobSystemInstances>
</LobSystemInstances>
<Entities>
<!-- individual <ENTITY> from every XML file goes here -->
</Entities>
</LobSystem>
</LobSystems>
</Model>
我要做的就是从给定文件夹中的每个 XML 文件中复制节点 <Entity>,并使用模板和信息创建一个新的 MASTER.XML,并从文件夹中的每个 .xml 中提取 <ENTITY>,结果这个:
<?xml version="1.0" encoding="utf-8"?>
<Model>
<LobSystems>
<LobSystem>
<LobSystemInstances>
</LobSystemInstances>
<Entities>
<Entity Name="A"> // from File1.XML
<Value>XYZ</Value>
</Entity>
<Entity Name="B"> // from File4.XML
<Value>123</Value>
</Entity>
<Entity Name="C"> // from File3.XML
<Value>@#$</Value>
</Entity>
</Entities>
</LobSystem>
</LobSystems>
</Model>
到目前为止,我的脚本如下:
[xml]$master = get-content .\Template.txt
$files = get-item -Path .\*.xml -Exclude 'Master.xml'
foreach ($file in $files)
{
[xml]$filecontents = get-content $file
$entity = $fileContents.Model.LobSystems.LobSystem.Entities.Entity
$master.Model.LobSystems.LobSystem.Entities.Entity.AppendChild($entity);
}
$master.Save("Master.xml")
它不工作....我不断收到有关 AppendChild() 的错误消息
有什么建议吗???
【问题讨论】:
-
我建议:定义不起作用...,在您的帖子中包含错误消息。
-
您不能在空值表达式上调用方法。在 Parser.ps1:17 char:5 + $master.Model.LobSystems.LobSystem.Entities.Entity.AppendChild($importedEnti ... + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
标签: xml powershell nodes