473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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
6 1184
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jean-gert nesselbosch | last post by:
hello everybody, does anybody know of standardization efforts concerning the output of w3cSchema/relaxNG-validating parsers ? I use libxml2 for validation-purposes (w3c-Schema and relaxNG) and...
3
by: Uwe Kuhne | last post by:
I receive a XML file from a customer and want to use a schema for validating the XML file. What I don't know how to do is, that I want to continue processing this XML - file no matter if there is...
1
by: Andy | last post by:
I am having some trouble validating XML using the XmlValidatingReader. I have created some xml and used the visual studio to generate the schema. So I am confident that the xml and schema match. ...
2
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
1
by: Chris Lieb | last post by:
I have an XML Schema file that I know is correct becuase I currently use it in a VB6 program to validate XML documents. Also, if I load an XML file into VS2005 that is not valid against this...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: Mahain | last post by:
I have to validate xml file against xsd file using c#. XmlReader only check for the well formated doucment but i want to check for every thing like space,datatype and any extra text in xml file. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.