我想做什么:
运行脚本并从XML文件加载内容。我有一个以上的XML文件,我希望它加载所有XML文件,然后在foreach
循环中运行它们。
我不知道为什么,但我的第二个Get-Content
总是空着。
以下是代码中不起作用的关键部分:
[xml]$xmlist = Get-Content "C:\Users\user\Desktop\Test.xml"
$x = $xmlist.Test.Test1.Test2
foreach ($newxml in $x) {
[xml]$xml1 = Get-Content $newxml
$x2 = $xml1.Test.Testa.Testb
}
$xml1
只是保持空白,没有任何内容被加载到其中(在$newxml
中我每次都有正确的路径。)
我得到的错误:
Get-Content : Cannot find path 'C:\windows\system32\System.Xml.XmlElement' because it does not exist. At C:\Users\samueu\Desktop\PS Script\Untitled15.ps1:7 char:14 + [xml]$xml1 = Get-Content $newxml + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\windows\syst....Xml.XmlElement:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
这是XML:
<?xml version="1.0"?>
<Test>
<Test1>
<Test2>
<Filepath>"C:\Users\user\Desktop\a.xml"</Filepath>
</Test2>
<Test2>
<Filepath>"C:\Users\user\Desktop\b.xml"</Filepath>
</Test2>
<Test2>
<Filepath>"C:\Users\user\Desktop\c.xml"</Filepath>
</Test2>
<Test2>
<Filepath>"C:\Users\user\Desktop\e.xml"</Filepath>
</Test2>
</Test1>
</Test>
答案 0 :(得分:2)
$newxml
包含XML节点,而不是字符串或FileInfo
对象。 XML节点对象的字符串表示形式为System.Xml.XmlElement
,但当前工作目录(C:\windows\system32
)中不存在具有该名称的文件。
您需要使用Get-Content
子节点的值调用<Filepath>
:
[xml]$xml1 = Get-Content $newxml.Filepath