![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | xml - xsd validation in powershell.... Hi There! I would like to create a xml - xsd validation function for Powershell, which would be a nifty addition to my function collection. I'm however not really handy with the whole .NET integration...... All I have cooked up is this.... function validate-xml([string] $path, [string] $path2){ trap{ write-host "An error occured: " write-host "ID: " $_.ErrorID write-host "Message: "$_.Exception.Message Exit 1 } $schemafile = get-item $path $xmlfile = get-item $path2 $xmldoc = [xml][string]::join("`n",(gc $xmlfile.FullName)) $schemacol = New-Object System.Xml.Schema.XmlSchemaCollection $schemacol.Add($null, $schemafile.FullName) $xmlvalidator = New-Object System.Xml.XmlValidatingReader $xmldoc,"Document",$null $xmlvalidator.Schemas.Add($schemacol) $xmlvalidator.ValidationType = "Schema" } I'm trying to figure out how to create and insert the validation event handler in this single function - if possible... Any ideas??? Also when I try to use the XmlNodeType.Document and ValidationType.Schema enumerations in the function above I keep running into the error (even if I use the complete namespace): Unable to find type [System.Xml.ValidationType.Schema]: make sure that the assembly containing this type is loaded. What is up with that? Regards, Mark |
My System Specs![]() |
| | #2 (permalink) |
| | Re: xml - xsd validation in powershell.... You can use a scriptblock to supply a delegate. Here's a script that validates some XML, a script block is passed to the add_ValidationEventHandler method. ############################# param($mySchema = "mySchema.xsd", $schemaURL = "http://mySchemaURL", $inputFile = "myInput.xml") $settings = new-object Xml.XmlReaderSettings $settings.ValidationFlags = [Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema $settings.ValidationType = [Xml.ValidationType]::Schema $settings.schemas.add($schemaURL, $mySchema) | out-null # add a scriptblock delegate $settings.add_ValidationEventHandler({ write-host "ERROR: line $($_.exception.LineNumber)" -nonewline write-host " position $($_.exception.LinePosition)" write-host $_.message }) $reader = [Xml.XmlReader]::Create($inputFile, $settings) while($reader.Read()) { } $reader.close() ################################# To access the members of enumerations, use this syntax: [System.Xml.ValidationType]::Schema [System.Xml.XmlNodeType]: ocument-- Nigel Sharples [MSFT] Windows PowerShell Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "mpriem" <spam@mpriem.com> wrote in message news:1173946897.610543.223750@d57g2000hsg.googlegroups.com... > Hi There! > > I would like to create a xml - xsd validation function for Powershell, > which would be a nifty addition to my function collection. > I'm however not really handy with the whole .NET integration...... > All I have cooked up is this.... > > function validate-xml([string] $path, [string] $path2){ > trap{ > write-host "An error occured: " > write-host "ID: " $_.ErrorID > write-host "Message: "$_.Exception.Message > Exit 1 > } > $schemafile = get-item $path > $xmlfile = get-item $path2 > $xmldoc = [xml][string]::join("`n",(gc $xmlfile.FullName)) > $schemacol = New-Object System.Xml.Schema.XmlSchemaCollection > $schemacol.Add($null, $schemafile.FullName) > $xmlvalidator = New-Object System.Xml.XmlValidatingReader > $xmldoc,"Document",$null > $xmlvalidator.Schemas.Add($schemacol) > $xmlvalidator.ValidationType = "Schema" > } > > I'm trying to figure out how to create and insert the validation event > handler in this single function - if possible... > Any ideas??? > > Also when I try to use the XmlNodeType.Document and > ValidationType.Schema enumerations in the function above I keep > running into the error (even if I use the complete namespace): > Unable to find type [System.Xml.ValidationType.Schema]: make sure that > the assembly containing this type is loaded. > What is up with that? > > Regards, > > Mark > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: xml - xsd validation in powershell.... "mpriem" <spam@mpriem.com> wrote in message news:1173946897.610543.223750@d57g2000hsg.googlegroups.com... > Hi There! > > I would like to create a xml - xsd validation function for Powershell, > which would be a nifty addition to my function collection. > I'm however not really handy with the whole .NET integration...... > All I have cooked up is this.... FYI the PowerShell Community Extensions project contains several XML related cmdlets that you might find handy: Test-Xml - tests well formed and if provided with schema, then tests against the schema Convert-Xml - does XSLT transforms Format-Xml - pretty print for XML http://www.codeplex.com/PowerShellCX -- Keith |
My System Specs![]() |
| | #4 (permalink) |
| | Re: xml - xsd validation in powershell.... Thanks guys!! I looked at the community extensions, but ran into issues when deploying/using them with the 1.0 version of Powershell. Did not have time to troubleshoot it. Can you advise some documentation on accessing the .Net framework??? Cheers, Mark |
My System Specs![]() |
| | #5 (permalink) |
| | Re: xml - xsd validation in powershell.... "mpriem" <spam@mpriem.com> wrote in message news:1174383089.588884.254410@o5g2000hsb.googlegroups.com... > Thanks guys!! > > I looked at the community extensions, but ran into issues when > deploying/using them with the 1.0 version of Powershell. Did not have > time to troubleshoot it. Are you sure you are running the RTM version of PowerShell. Almost everyone who has run into PSCX 1.1 install issues was on a release candidate of PowerShell. > Can you advise some documentation on accessing the .Net framework??? Your best bet is to pick up Windows PowerShell In Action and check out chapter 11. It covers accessing the .NET framework and gives it a pretty good treatment: http://www.amazon.com/Windows-Powers...4415395&sr=8-1 -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Validation? Where did this come from? | General Discussion | |||
| Validation | Vista installation & setup | |||
| Validation | Vista installation & setup | |||
| Validation | Vista installation & setup | |||
| validation | Vista General | |||