What type of exception should Powershell use to detect XML parsing errors due to invalid characters?

Line 2 in the script below generates -

“Cannot convert the value“ System.Object [] ”to the input“ System.Xml.XmlDocument. ”Error:“ '→', the hex value 0x1A, is an invalid character. Line 39, position 23. "

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

What exception should be indicated on line 4 (script) in order to catch the above 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 watching (and responding)

Adrian

+6
source share
2 answers

Here's a way to find out the full name of the type of exception, the result is a System.Management.Automation.ArgumentTransformationMetadataException 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 } 
+5
source

System.Management.Automation.ArgumentTransformationMetadataException

+3
source

Source: https://habr.com/ru/post/914595/


All Articles