Skip to content

Check-XmlFile

Verifies an XML file

param([string]$path = "")

try {
	if ($path -eq "" ) { $path = Read-Host "Enter path to XML file" }

	$XmlFile = Get-Item $path
	
	# Perform the XSD Validation
	$script:ErrorCount = 0
	$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++ })
	$ReaderSettings.DtdProcessing = [System.Xml.DtdProcessing]::Parse
	$Reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $ReaderSettings)
	while ($Reader.Read()) { }
	$Reader.Close()
	
	if ($script:ErrorCount -gt 0) {	throw "Invalid XML" } 

	"? $path is valid XML"
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the path to the XML file

An interactive directory of PowerShell scripts.