I've got a script that loads numerous XML files from a directory, runs various data entry checks on each one, and then generates a report of all the errors. It runs fine except when it runs into invalid data (such as an ampersand).
For example, if my XML file contains something like this:
I get an error like this in the console:
This leads me to two questions:
1. How do I force my script to continue processing the files instead of stopping at the problem file?
2. Is there a way to output the error message to my report file instead of the console?
In case it helps, this is a basic version of my script:
For example, if my XML file contains something like this:
Code:
<Name>This & That</Name>
Exception calling "Load" with "1" argument(s): "An error occurred while parsing EntityName. Line 40, position 37."
At :line:8 char:9
+ $xd.load <<<< ($file)
This leads me to two questions:
1. How do I force my script to continue processing the files instead of stopping at the problem file?
2. Is there a way to output the error message to my report file instead of the console?
In case it helps, this is a basic version of my script:
Code:
cls #clear the console screen
[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
$report = "X:\XML_CHECK.txt" #set the file you want to use for the results
Clear-Content $report #clears out everything so that you don't start off with old results
Foreach ($file in Get-Childitem X:\Test\*.xml)
{
$xd.load($file)
$company = $xd.DocumentElement.SoldTo.Company.Name
[string] $invoicenumber = $file.BaseName
switch ($company)
{
"Company A"
{Write-Output "$invoicenumber - $company" | Out-File $report -Append}
"Company B"
{Write-Output "$invoicenumber - $company" | Out-File $report -Append}
default
{Write-Output "$invoicenumber - $company is not an electronic invoicing customer." | Out-File $report -Append}
}
}
notepad $report