function New-XsdFromXml{<#.NOTES.Author Will Steele (will.steele@live.com).Last Modified Date: 2012/08/11.SYNOPSISCreate XSD file from XML..EXAMPLENew-XsdFromXml -Xml C:data est.xml -Xsd C:data est.xsd.PARAMETER XmlA required string indicating a file path to an .xml file..PARAMETER XsdA required string indicating a file path to an output .xsd file..LINKhttp://learningpcs.blogspot.com/2012/08/powershell-v3-function-new-xsdfromxml.html#>[CmdletBinding()]param($Xml,$Xsd)# Read xml$reader = [System.Xml.XmlReader]::Create($Xml)# Instntiate XmlSchemaSet and XmlSchemaInference to process new XSD$schemaSet = New-Object System.Xml.Schema.XmlSchemaSet$schema
10.0pt;">= New-Object System.Xml.Schema.XmlSchemaInference
# Infer schemaSet from XML document in $reader
$schemaSet = $schema.InferSchema($reader);
# Create new output file
$file = New-Object System.IO.FileStream($Xsd, [IO.FileMode]::CreateNew)
# Create XmlTextWriter with UTF8 Encoding to write to file
$xwriter = New-Object System.Xml.XmlTextWriter($file, [Text.Encoding]::UTF8)
# Set formatting to indented
$xwriter.Formatting = [System.Xml.Formatting]::Indented
# Parse SchemaSet objects
$schemaSet.Schemas() |
ForEach-Object {
[System.Xml.Schema.XmlSchema] $_.Write($xwriter)
}
# Close objects with handles on file
$xwriter.Close()
$reader.Close()
}