Skip to main content
function updated
Source Link
emekm
  • 772
  • 6
  • 9

the solution of (Flatliner DOA) is working good on PSv2, but not on Server 2012 PSv3.

the solution of (wangzq) is working on PS2 and PS3!!

anyone who needs an xml validation on PS3, can use this (based on wangzq's function)

function Test-Xml {
    param (
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $XmlFile,

        [Parameter(Mandatory=$true)]
        [string] $SchemaFile,
    )

    [string[]]$Script:XmlValidationErrorLog = @()
    [scriptblock] $ValidationEventHandler = { 
 Write-Error       $Script:XmlValidationErrorLog += $args[1].Exception.Message
    }
    )

    $xml = New-Object System.Xml.XmlDocument
    $schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile
    $schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler)
    $xml.Schemas.Add($schema) | Out-Null
    $xml.Load($XmlFile)
    $xml.Validate($ValidationEventHandler)
    
    if ($Script:XmlValidationErrorLog) {
        Write-Warning "$($Script:XmlValidationErrorLog.Count) errors found"
        Write-Error "$Script:XmlValidationErrorLog"
    }
    else {
        Write-Host "The script is valid"
    }
}

Test-Xml -XmlFile $XmlFile -SchemaFile $SchemaFile

the solution of (Flatliner DOA) is working good on PSv2, but not on Server 2012 PSv3.

the solution of (wangzq) is working on PS2 and PS3!!

anyone who needs an xml validation on PS3, use this

function Test-Xml {
    param (
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $XmlFile,

        [Parameter(Mandatory=$true)]
        [string] $SchemaFile,

        [scriptblock] $ValidationEventHandler = { Write-Error $args[1].Exception }
    )

    $xml = New-Object System.Xml.XmlDocument
    $schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile
    $schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler)
    $xml.Schemas.Add($schema) | Out-Null
    $xml.Load($XmlFile)
    $xml.Validate($ValidationEventHandler)
}
Test-Xml -XmlFile $XmlFile -SchemaFile $SchemaFile

the solution of (Flatliner DOA) is working good on PSv2, but not on Server 2012 PSv3.

the solution of (wangzq) is working on PS2 and PS3!!

anyone who needs an xml validation on PS3, can use this (based on wangzq's function)

function Test-Xml {
    param (
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $XmlFile,

        [Parameter(Mandatory=$true)]
        [string] $SchemaFile
    )

    [string[]]$Script:XmlValidationErrorLog = @()
    [scriptblock] $ValidationEventHandler = { 
        $Script:XmlValidationErrorLog += $args[1].Exception.Message
    }
    
    $xml = New-Object System.Xml.XmlDocument
    $schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile
    $schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler)
    $xml.Schemas.Add($schema) | Out-Null
    $xml.Load($XmlFile)
    $xml.Validate($ValidationEventHandler)
    
    if ($Script:XmlValidationErrorLog) {
        Write-Warning "$($Script:XmlValidationErrorLog.Count) errors found"
        Write-Error "$Script:XmlValidationErrorLog"
    }
    else {
        Write-Host "The script is valid"
    }
}

Test-Xml -XmlFile $XmlFile -SchemaFile $SchemaFile
Source Link
emekm
  • 772
  • 6
  • 9

the solution of (Flatliner DOA) is working good on PSv2, but not on Server 2012 PSv3.

the solution of (wangzq) is working on PS2 and PS3!!

anyone who needs an xml validation on PS3, use this

function Test-Xml {
    param (
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $XmlFile,

        [Parameter(Mandatory=$true)]
        [string] $SchemaFile,

        [scriptblock] $ValidationEventHandler = { Write-Error $args[1].Exception }
    )

    $xml = New-Object System.Xml.XmlDocument
    $schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile
    $schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler)
    $xml.Schemas.Add($schema) | Out-Null
    $xml.Load($XmlFile)
    $xml.Validate($ValidationEventHandler)
}
Test-Xml -XmlFile $XmlFile -SchemaFile $SchemaFile