|
Prev: Help with System.Data.OleDb code...
Next: Serial Port
From: SurturZ on 30 Jun 2008 22:23 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(mstrValidateXmlFileErrorMessage)) 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...(a)sbcglobal.net> wrote: > > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/cf1b61b8b513dfbc > > Thanks, > > Seth Rowe [MVP] > http://sethrowe.blogspot.com/ >
From: Cor Ligthert[MVP] on 30 Jun 2008 23:35 SuturZ, Can you explain what it does more then my simple line of code in a Try and Catch? Cor "SurturZ" <surturz(a)newsgroup.nospam> schreef in bericht news:1B8B602F-F520-44CE-BDF6-10B273BB5DFD(a)microsoft.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(mstrValidateXmlFileErrorMessage)) > 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...(a)sbcglobal.net> wrote: >> > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/cf1b61b8b513dfbc >> >> Thanks, >> >> Seth Rowe [MVP] >> http://sethrowe.blogspot.com/ >>
From: SurturZ on 1 Jul 2008 01:10 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" <surturz(a)newsgroup.nospam> schreef in bericht > news:1B8B602F-F520-44CE-BDF6-10B273BB5DFD(a)microsoft.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(mstrValidateXmlFileErrorMessage)) > > 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...(a)sbcglobal.net> wrote: > >> > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/cf1b61b8b513dfbc > >> > >> Thanks, > >> > >> Seth Rowe [MVP] > >> http://sethrowe.blogspot.com/ > >> >
From: SurturZ on 1 Jul 2008 22:01 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" <surturz(a)newsgroup.nospam> schreef in bericht > > news:1B8B602F-F520-44CE-BDF6-10B273BB5DFD(a)microsoft.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(mstrValidateXmlFileErrorMessage)) > > > 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...(a)sbcglobal.net> wrote: > > >> > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/cf1b61b8b513dfbc > > >> > > >> Thanks, > > >> > > >> Seth Rowe [MVP] > > >> http://sethrowe.blogspot.com/ > > >> > >
|
Pages: 1 Prev: Help with System.Data.OleDb code... Next: Serial Port |