应该在 Powershell 中使用什么异常类型来捕获由于无效字符导致的 XML 解析错误? [英] What exception type should be used in Powershell to catch a XML parse error due to invalid characters?
问题描述
下面脚本中的第 2 行生成 -
Line 2 in the script below generates -
无法将值System.Object[]"转换为类型System.Xml.XmlDocument".错误:'→',十六进制值 0x1A,是无效字符.第 39 行,第 23 位."
"Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'→', hexadecimal value 0x1A, is an invalid character. Line 39, position 23."
在 line:1 char:8+ [xml]$x <<<<= 获取内容 4517.xml+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException+ FullyQualifiedErrorId : RuntimeException"
At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException"
应该在(脚本的)第 4 行指定什么异常来捕获上述错误?
What exception should be specified on line 4 (of the script) to catch the aforementioned error?
try {
[xml]$xml = Get-Content $file # line 2
}
catch [?] { # line 4
echo "XML parse error!"
# handle the parse error differently
}
catch {
echo $error
# some general error
}
感谢您的关注(和回答)
Thanks for looking (and answering)
阿德里安
推荐答案
这是一种发现异常的完整类型名称的方法,这里的结果给出了@Adrian Wright 给出的 System.Management.Automation.ArgumentTransformationMetadataException.
Here is a way to discover yourself the full type name of an Exception, the result here gives System.Management.Automation.ArgumentTransformationMetadataException as given by @Adrian Wright.
Clear-Host
try {
[xml]$xml = Get-Content "c:Temp1.cs" # line 2
}
catch {
# Discovering the full type name of an exception
Write-Host $_.Exception.gettype().fullName
Write-Host $_.Exception.message
}
这篇关于应该在 Powershell 中使用什么异常类型来捕获由于无效字符导致的 XML 解析错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!