By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

validating if a file is a true valid xml file that fits a certain schema

P: n/a
I need to make sure that a file saved in a particular place is a valid xml
file that fits a certain schema. Where would I get started doing this? The
original file would have been created and saved with a dataset.

Jun 30 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Andy,

Just set it in a Try and Catch block with a dataset.ReadXML

Cor

"Andy B" <a_*****@sbcglobal.netschreef in bericht
news:us**************@TK2MSFTNGP06.phx.gbl...
>I need to make sure that a file saved in a particular place is a valid xml
file that fits a certain schema. Where would I get started doing this? The
original file would have been created and saved with a dataset.
Jun 30 '08 #2

P: n/a
On Jun 29, 9:09*pm, "Andy B" <a_bo...@sbcglobal.netwrote:
I need to make sure that a file saved in a particular place is a valid xml
file that fits a certain schema. Where would I get started doing this? The
original file would have been created and saved with a dataset.
Replies to this in your other thread:

http://groups.google.com/group/micro...1b61b8b513dfbc

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jun 30 '08 #3

P: n/a
It's pretty fiddly, here's my code:

''' <summary>
''' Validates an XML stream against the specified XML Schema. Raises
ValidateXmlFileException if the XML stream or XML Schema fails validation.
''' </summary>
''' <param name="SchemaNamespace"></param>
''' <param name="SchemaStream"></param>
''' <param name="XmlStream"></param>
''' <exception cref="ValidateXMLFileException">Raised if XML or XML
File fails validation</exception>
''' <remarks></remarks>
Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ByVal
SchemaStream As Stream, ByVal XmlStream As Stream)

' Create the XmlSchemaSet class.
Dim sc As XmlSchemaSet = New XmlSchemaSet()

Try
' Add the schema to the collection.
Dim xrInput As New XmlTextReader(SchemaStream)
sc.Add(SchemaNamespace, xrInput)

' Set the validation settings.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
' Create the XmlReader object.
Using reader As XmlReader = XmlReader.Create(XmlStream,
settings)
' Parse the file.
Do While reader.Read() : Loop
End Using
If mstrValidateXmlFileErrorMessage "" Then
Throw New ValidateXMLFileException("The XML Stream
failed validation", New ApplicationException(mstrValidateXmlFileErrorMessa ge))
End If
RemoveHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
Catch ex As XmlSchemaException
Throw New ValidateXMLFileException("Could not parse schema",
ex)
Catch ex As XmlException
Throw New ValidateXMLFileException("Could not parse XML", ex)
End Try

End Sub
Private Sub ValidateXmlFile_Callback(ByVal sender As Object, ByVal e
As ValidationEventArgs)
mstrValidateXmlFileErrorMessage = e.Message
End Sub
Public Class ValidateXMLFileException
Inherits Exception
Sub New(ByVal ErrorMessage As String, ByVal InnerException As
Exception)
MyBase.New(ErrorMessage, InnerException)
End Sub
End Class

--
David Streeter
Synchrotech Software
Sydney Australia
"rowe_newsgroups" wrote:
On Jun 29, 9:09 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
I need to make sure that a file saved in a particular place is a valid xml
file that fits a certain schema. Where would I get started doing this? The
original file would have been created and saved with a dataset.

Replies to this in your other thread:

http://groups.google.com/group/micro...1b61b8b513dfbc

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 1 '08 #4

P: n/a

SuturZ,

Can you explain what it does more then my simple line of code in a Try and
Catch?

Cor

"SurturZ" <su*****@newsgroup.nospamschreef in bericht
news:1B**********************************@microsof t.com...
It's pretty fiddly, here's my code:

''' <summary>
''' Validates an XML stream against the specified XML Schema.
Raises
ValidateXmlFileException if the XML stream or XML Schema fails validation.
''' </summary>
''' <param name="SchemaNamespace"></param>
''' <param name="SchemaStream"></param>
''' <param name="XmlStream"></param>
''' <exception cref="ValidateXMLFileException">Raised if XML or XML
File fails validation</exception>
''' <remarks></remarks>
Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ByVal
SchemaStream As Stream, ByVal XmlStream As Stream)

' Create the XmlSchemaSet class.
Dim sc As XmlSchemaSet = New XmlSchemaSet()

Try
' Add the schema to the collection.
Dim xrInput As New XmlTextReader(SchemaStream)
sc.Add(SchemaNamespace, xrInput)

' Set the validation settings.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
' Create the XmlReader object.
Using reader As XmlReader = XmlReader.Create(XmlStream,
settings)
' Parse the file.
Do While reader.Read() : Loop
End Using
If mstrValidateXmlFileErrorMessage "" Then
Throw New ValidateXMLFileException("The XML Stream
failed validation", New
ApplicationException(mstrValidateXmlFileErrorMessa ge))
End If
RemoveHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
Catch ex As XmlSchemaException
Throw New ValidateXMLFileException("Could not parse
schema",
ex)
Catch ex As XmlException
Throw New ValidateXMLFileException("Could not parse XML",
ex)
End Try

End Sub
Private Sub ValidateXmlFile_Callback(ByVal sender As Object, ByVal
e
As ValidationEventArgs)
mstrValidateXmlFileErrorMessage = e.Message
End Sub
Public Class ValidateXMLFileException
Inherits Exception
Sub New(ByVal ErrorMessage As String, ByVal InnerException As
Exception)
MyBase.New(ErrorMessage, InnerException)
End Sub
End Class

--
David Streeter
Synchrotech Software
Sydney Australia
"rowe_newsgroups" wrote:
>On Jun 29, 9:09 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
I need to make sure that a file saved in a particular place is a valid
xml
file that fits a certain schema. Where would I get started doing this?
The
original file would have been created and saved with a dataset.

Replies to this in your other thread:

http://groups.google.com/group/micro...1b61b8b513dfbc

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 1 '08 #5

P: n/a
My code tests against a supplied XML Schema.

I don't know if Dataset.ReadXML does that. I've never used Dataset.ReadXML
though, perhaps it is a better approach.

BTW I think in my original post I missed a module level String variable
declaration:

Private mstrValidateXmlFileErrorMessage As String 'used by ValidateXmlFile()

--
David Streeter
Synchrotech Software
Sydney Australia
"Cor Ligthert[MVP]" wrote:
>
SuturZ,

Can you explain what it does more then my simple line of code in a Try and
Catch?

Cor

"SurturZ" <su*****@newsgroup.nospamschreef in bericht
news:1B**********************************@microsof t.com...
It's pretty fiddly, here's my code:

''' <summary>
''' Validates an XML stream against the specified XML Schema.
Raises
ValidateXmlFileException if the XML stream or XML Schema fails validation.
''' </summary>
''' <param name="SchemaNamespace"></param>
''' <param name="SchemaStream"></param>
''' <param name="XmlStream"></param>
''' <exception cref="ValidateXMLFileException">Raised if XML or XML
File fails validation</exception>
''' <remarks></remarks>
Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ByVal
SchemaStream As Stream, ByVal XmlStream As Stream)

' Create the XmlSchemaSet class.
Dim sc As XmlSchemaSet = New XmlSchemaSet()

Try
' Add the schema to the collection.
Dim xrInput As New XmlTextReader(SchemaStream)
sc.Add(SchemaNamespace, xrInput)

' Set the validation settings.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
' Create the XmlReader object.
Using reader As XmlReader = XmlReader.Create(XmlStream,
settings)
' Parse the file.
Do While reader.Read() : Loop
End Using
If mstrValidateXmlFileErrorMessage "" Then
Throw New ValidateXMLFileException("The XML Stream
failed validation", New
ApplicationException(mstrValidateXmlFileErrorMessa ge))
End If
RemoveHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
Catch ex As XmlSchemaException
Throw New ValidateXMLFileException("Could not parse
schema",
ex)
Catch ex As XmlException
Throw New ValidateXMLFileException("Could not parse XML",
ex)
End Try

End Sub
Private Sub ValidateXmlFile_Callback(ByVal sender As Object, ByVal
e
As ValidationEventArgs)
mstrValidateXmlFileErrorMessage = e.Message
End Sub
Public Class ValidateXMLFileException
Inherits Exception
Sub New(ByVal ErrorMessage As String, ByVal InnerException As
Exception)
MyBase.New(ErrorMessage, InnerException)
End Sub
End Class

--
David Streeter
Synchrotech Software
Sydney Australia
"rowe_newsgroups" wrote:
On Jun 29, 9:09 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
I need to make sure that a file saved in a particular place is a valid
xml
file that fits a certain schema. Where would I get started doing this?
The
original file would have been created and saved with a dataset.

Replies to this in your other thread:

http://groups.google.com/group/micro...1b61b8b513dfbc

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 1 '08 #6

P: n/a
I just had a play around with the Dataset XML functions. They are VERY good,
and are the better solution for the OP.

However, they seem to work only for XML/XSDs that have been generated from a
Dataset. If you are sourcing your XML/XSD from a different source, then my
code might be necessary (it can validate any XML file against any schema).

--
David Streeter
Synchrotech Software
Sydney Australia
"SurturZ" wrote:
My code tests against a supplied XML Schema.

I don't know if Dataset.ReadXML does that. I've never used Dataset.ReadXML
though, perhaps it is a better approach.

BTW I think in my original post I missed a module level String variable
declaration:

Private mstrValidateXmlFileErrorMessage As String 'used by ValidateXmlFile()

--
David Streeter
Synchrotech Software
Sydney Australia
"Cor Ligthert[MVP]" wrote:

SuturZ,

Can you explain what it does more then my simple line of code in a Try and
Catch?

Cor

"SurturZ" <su*****@newsgroup.nospamschreef in bericht
news:1B**********************************@microsof t.com...
It's pretty fiddly, here's my code:
>
''' <summary>
''' Validates an XML stream against the specified XML Schema.
Raises
ValidateXmlFileException if the XML stream or XML Schema fails validation.
''' </summary>
''' <param name="SchemaNamespace"></param>
''' <param name="SchemaStream"></param>
''' <param name="XmlStream"></param>
''' <exception cref="ValidateXMLFileException">Raised if XML or XML
File fails validation</exception>
''' <remarks></remarks>
Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ByVal
SchemaStream As Stream, ByVal XmlStream As Stream)
>
' Create the XmlSchemaSet class.
Dim sc As XmlSchemaSet = New XmlSchemaSet()
>
Try
' Add the schema to the collection.
Dim xrInput As New XmlTextReader(SchemaStream)
sc.Add(SchemaNamespace, xrInput)
>
' Set the validation settings.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
' Create the XmlReader object.
Using reader As XmlReader = XmlReader.Create(XmlStream,
settings)
' Parse the file.
Do While reader.Read() : Loop
End Using
If mstrValidateXmlFileErrorMessage "" Then
Throw New ValidateXMLFileException("The XML Stream
failed validation", New
ApplicationException(mstrValidateXmlFileErrorMessa ge))
End If
RemoveHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
Catch ex As XmlSchemaException
Throw New ValidateXMLFileException("Could not parse
schema",
ex)
Catch ex As XmlException
Throw New ValidateXMLFileException("Could not parse XML",
ex)
End Try
>
End Sub
Private Sub ValidateXmlFile_Callback(ByVal sender As Object, ByVal
e
As ValidationEventArgs)
mstrValidateXmlFileErrorMessage = e.Message
End Sub
Public Class ValidateXMLFileException
Inherits Exception
Sub New(ByVal ErrorMessage As String, ByVal InnerException As
Exception)
MyBase.New(ErrorMessage, InnerException)
End Sub
End Class
>
--
David Streeter
Synchrotech Software
Sydney Australia
>
>
"rowe_newsgroups" wrote:
>
>On Jun 29, 9:09 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
I need to make sure that a file saved in a particular place is a valid
xml
file that fits a certain schema. Where would I get started doing this?
The
original file would have been created and saved with a dataset.
>>
>Replies to this in your other thread:
>>
>http://groups.google.com/group/micro...1b61b8b513dfbc
>>
>Thanks,
>>
>Seth Rowe [MVP]
>http://sethrowe.blogspot.com/
>>
Jul 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.