Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - xml - xsd validation in powershell....

Reply
 
Old 03-15-2007   #1 (permalink)
mpriem


 
 

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 SpecsSystem Spec
Old 03-16-2007   #2 (permalink)
Nigel Sharples


 
 

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 SpecsSystem Spec
Old 03-16-2007   #3 (permalink)
Keith Hill


 
 

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 SpecsSystem Spec
Old 03-20-2007   #4 (permalink)
mpriem


 
 

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 SpecsSystem Spec
Old 03-20-2007   #5 (permalink)
Keith Hill


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46