Added validate_xml.ps1

This commit is contained in:
Markus Fleschutz 2020-09-30 15:25:52 +02:00
parent 18a7eb28dc
commit ac04c83499
2 changed files with 31 additions and 0 deletions

View File

@ -24,6 +24,7 @@ PowerShell Scripts Included
* [train_dns_cache.ps1](Scripts/train_dns_cache.ps1) - trains the DNS cache with frequently used domain names
* [translate.ps1](Scripts/translate.ps1) - translates the given text
* [txt2wav.ps1](Scripts/txt2wav.ps1) - converts text into a audio .WAV file
* [validate_xml.ps1](Scripts/validate_xml.ps1) - validates the given XML file
* [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast
* [wakeup.ps1](Scripts/wakeup.ps1) - sends a magic packet to the given computer, waking him up
* [zipdir.ps1](Scripts/zipdir.ps1) - creates a zip archive of the given folder

30
Scripts/validate_xml.ps1 Normal file
View File

@ -0,0 +1,30 @@
function Validate-Xml {
param ([string]$XmlFilePath)
try {
# Get the file
$XmlFile = Get-Item($XmlFilePath)
# Keep count of how many errors there are in the XML file
$script:ErrorCount = 0
# Perform the XSD Validation
$ReaderSettings = New-Object -TypeName System.Xml.XmlReaderSettings
$ReaderSettings.ValidationType = [System.Xml.ValidationType]::Schema
$ReaderSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation
$ReaderSettings.add_ValidationEventHandler({ $script:ErrorCount++ })
$Reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $ReaderSettings)
while ($Reader.Read()) { }
$Reader.Close()
# Verify the results of the XSD validation
if ($script:ErrorCount -gt 0) {
# XML is NOT valid
$false
} else {
# XML is valid
$true
}
} catch {
Write-Warning "$($MyInvocation.MyCommand.Name) - Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
}
}