PowerShell获取最新XML文件并解析时遇类型转换错误求助
问题
尝试用PowerShell从文件夹获取最新XML文件并解析数据,能成功定位到文件,但执行时出现以下错误:
Cannot convert value "ResultFile_10.xml" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type." At line:1 char:1 + [xml]$sdfd = Get-ChildItem C:\PowershellCode -File | Sort-Object Last ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException
出错的代码
[xml]$sdfd = Get-ChildItem C:\PowershellCode -File | Sort-Object LastWriteTime -Descending| Select-Object -First 1 $sdfd.testsuites.testsuite | ForEach-Object { [pscustomobject]@{ TestSuiteName = $_.name TotalExecutionList = $_.tests Failures = $_.failures } }
可正常运行的对比代码
直接指定单个文件路径,用Get-Content读取内容时能正常解析:
[xml]$xmlAttr = Get-Content -Path D:\Agent_2.xml $xmlAttr.testsuites.testsuite | ForEach-Object { [pscustomobject]@{ TestSuiteName = $_.name TotalExecutionList = $_.tests Failures = $_.failures } }
成功运行输出
TestSuiteName=Open Mail; TotalExecutionList=2; Failures=0
XML示例文件内容
<?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite name="Open Google" tests="2" failures="0" skipped="0" time="30.0730982" timestamp="2022-10-21T05:24:31" id="3a03345d-9024-6c4a-d9be-56ca477ffd21" log=""> <testcase name="Opengoogle" time="6380192668.742" timestamp="" log="+ Passed Opengoogle
+ Passed OpenUrl

" /> <testcase name="Print the PDF Report" time="63801926694.7071" timestamp="" log="+ Passed Print the PDF Report
+ Passed Print PDF Report Of TestCases {LogInfo='Started 'C:\Tosca_Projects\TCShell\Execution.bat' with arguments 'General Google "/Uesrs/Deepak Shaw/CICD/DEX/TVIA/Open Google"''}

" /> </testsuite> <testsuite name="Open Mail" tests="2" failures="0" skipped="0" time="16.8880552" timestamp="2022-10-21T05:25:36" id="3a0335a8-f76b-3d18-f1d2-82680602c3d0" log=""> <testcase name="Open Gmail" time="63801926742.2266" timestamp="" log="+ Passed Open Gmail
+ Passed OpenUrl

" /> <testcase name="Print the PDF Report" time="63801926746.5905" timestamp="" log="+ Passed Print the PDF Report
+ Passed Print PDF Report Of TestCases {LogInfo='Started 'C:\Tosca_Projects\TCShell\Execution.bat' with arguments 'General OpenMailQA "/Uesrs/Deepak Shaw/CICD/DEX/TVIA/Open Mail"''}

" /> </testsuite> </testsuites>
解决方法
错误原因很明确:你把Get-ChildItem返回的**文件信息对象(FileInfo)**直接赋值给了[xml]类型变量,但[xml]需要的是XML文本内容,不是文件对象。
修改后的代码如下:
# 筛选文件夹中的XML文件,按修改时间倒序取最新的一个 $latestXml = Get-ChildItem C:\PowershellCode -File -Filter *.xml | Sort-Object LastWriteTime -Descending | Select-Object -First 1 # 读取该文件的内容并转换为XML对象 [xml]$sdfd = Get-Content -Path $latestXml.FullName # 解析并输出所需数据 $sdfd.testsuites.testsuite | ForEach-Object { [pscustomobject]@{ TestSuiteName = $_.name TotalExecutionList = $_.tests Failures = $_.failures } }
也可以写成更紧凑的单行形式:
[xml]$sdfd = Get-Content (Get-ChildItem C:\PowershellCode -File -Filter *.xml | Sort LastWriteTime -Descending | Select -First 1).FullName $sdfd.testsuites.testsuite | ForEach-Object { [pscustomobject]@{ TestSuiteName = $_.name TotalExecutionList = $_.tests Failures = $_.failures } }
关键注意点:
- 添加
-Filter *.xml可以只处理XML文件,避免其他类型文件干扰。 - 必须使用
FullName属性获取文件的完整路径,确保Get-Content能正确找到文件。 [xml]类型只能接收文本内容,必须通过Get-Content读取文件内容后再转换。
内容的提问来源于stack exchange,提问作者NIREN GUPTA
相关产品推荐
相关产品推荐

