来自另一个文件的PowerShell XML节点
我正在运行PowerShell脚本,基本上是从几个较小的XML文件创建一个较大的XML文件 脚本将打开Template.txt:来自另一个文件的PowerShell XML节点,xml,powershell,nodes,Xml,Powershell,Nodes,我正在运行PowerShell脚本,基本上是从几个较小的XML文件创建一个较大的XML文件 脚本将打开Template.txt: <?xml version="1.0" encoding="utf-8"?> <Model> <LobSystems> <LobSystem> <LobSystemInstances> </LobSystemInstances> <Enti
<?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>
嗯,它不起作用了。。。。我不断收到有关AppendChild()的错误消息
有什么建议吗?由于元素来自另一个XML文档实例,您似乎需要首先删除该元素,如下所示:
foreach ($file in $files)
{
[xml]$filecontents = get-content $file
$entity = $fileContents.Model.LobSystems.LobSystem.Entities.Entity
$importedEntity = $master.ImportNode($entity, $TRUE)
$master.Model.LobSystems.LobSystem.Entities.AppendChild($importedEntity);
}
我建议:定义不工作…,在帖子中包含错误消息。不能对空值表达式调用方法。在Parser.ps1:17中,字符:5+$master.Model.LobSystems.LobSystem.Entities.Entity.AppendChild($importedEnti…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,RuntimeException+FullyQualifiedErrorId:InvokeMethodOnNullWondeful!!!!让我找到了正确的方法,但我必须删除一个小错误,而不是Entities.Entity。只需删除。Entity和所有操作都很好!当然,
AppendChild()
应该在实体的父实体上调用。感谢确认和更正,答案已相应更新。
[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")
foreach ($file in $files)
{
[xml]$filecontents = get-content $file
$entity = $fileContents.Model.LobSystems.LobSystem.Entities.Entity
$importedEntity = $master.ImportNode($entity, $TRUE)
$master.Model.LobSystems.LobSystem.Entities.AppendChild($importedEntity);
}