Powershell中应该使用什么异常类型来捕获由于无效字符引起的XML解析错误? [英] What exception type should be used in Powershell to catch a XML parse error due to invalid characters?

查看:264
本文介绍了Powershell中应该使用什么异常类型来捕获由于无效字符引起的XML解析错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面脚本中的第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."

在行:1 char:8
+ [xml] $ x<<< = Get-内容4517.xml
+类别信息: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
}

感谢您寻找(和回答)

Adrian

推荐答案

这是一种发现自己的完整类型名称的异常,这里的结果给出了由@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:\Temp\1.cs" # line 2
}
catch {
    # Discovering the full type name of an exception
    Write-Host $_.Exception.gettype().fullName
    Write-Host $_.Exception.message
}

这篇关于Powershell中应该使用什么异常类型来捕获由于无效字符引起的XML解析错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆