6

Dòng 2 trong kịch bản dưới đây tạo -Loại ngoại lệ nào nên được sử dụng trong Powershell để bắt lỗi phân tích cú pháp XML do các ký tự không hợp lệ?

"Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'→', hexadecimal value 0x1A, is an invalid character. Line 39, position 23."

At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException"

ngoại lệ gì nên được chỉ định trên dòng 4 (của kịch bản) để bắt các lỗi nói trên?

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 
} 

Cám ơn tìm kiếm (và trả lời)

Adrian

Trả lời

5

Dưới đây là một cách để khám phá bản thân các loại tên đầy đủ của một ngoại lệ, các kết quả ở đây cho System.Management.Automation.ArgumentTransformationMetadataException như được đưa ra bởi @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 
} 
3

System.Management.Automation.ArgumentTransformationMetadataException