xmlpowershellschemaxsd-validation

Generate an xml schema from a variable (like a here-string)


Ultimately, I'm wondering if it's possible to validate an Xml file against a schema that's contained in a here-string variable (rather than an already-existing .xsd file).



Am I missing something, or am I stuck with having to create an .xsd file if I want to validate an XmlDocument instance against a schema?


Solution

  • System.Xml.Schema.XmlSchema has a Read method that allows you to take input from a System.IO.TextReader, and therefore from a System.IO.StringReader, and therefore from a String.

    $xmlSchemaText = @"
    <schema xml here>
    "@
    
    $stringReader = [System.IO.StringReader]::new($xmlSchemaText)
    
    $xmlSchema = [System.Xml.Schema.XmlSchema]::Read($stringReader)