locked
Dataset.ReadXmlSchema problem

    Question

  • Ok folks i've now got a flat fore head from all of the head banging on the wall I've been doing so I need your help!...

    I am trying to read in an xml document against a xml schema using the dataset.readxml and dataset.readxmlschema methods.  See code below...

    Public Sub ReadXML

    Dim settings As XmlReaderSettings

    Dim reader As XmlReader

    Dim strSchemaLocation As String

    Dim objDataSet As New DataSet

     

    ' First check the schema is where it should be

    If Not File.Exists(strSchemaLocation) Then

         Throw New BespokeException(Nothing, BespokeException.ErrorType.FILE_NOT_FOUND, "Cannot find file: '" & strSchemaLocation & "'")

    End If

    Try

         settings = New XmlReaderSettings

         settings.Schemas.Add(Nothing, XmlReader.Create(strSchemaLocation))

         settings.ValidationType = ValidationType.Schema

         settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings

         settings.IgnoreWhitespace = True

         settings.IgnoreComments = True

         AddHandler settings.ValidationEventHandler, AddressOf settings_ValidationEventHandler

         reader = XmlReader.Create(sourceURL, settings)

         objDataSet.ReadXmlSchema(reader)

         objDataSet.ReadXml(reader)

    Catch ex As Exception

         Throw New BespokeException(ex, BespokeException.ErrorType.XML_DOCUMENT_INVALID, "Could not read the XML Document. ErrorLogReader: tCollection(1)")

    End Try

    End Sub

    Private Shared Sub settings_ValidationEventHandler(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)

    MessageBox.Show("The Error Log Viewer could not open the specified file because: " & e.Message, "Invalid XML Document")

    End Sub

    As you can see the dataset is passed a schema to validate the xml file against and if the file is invalid, then a message will be displayed to say what is invalid. 

    Now I have checked that my xml document is valid by making it invalid and then watching as the ValidationEventHandler is executed and a message is shown.  When I run it against a valid xml document (with data/rows in it) the objDataset object tells me it has no rows but if I comment out the objDataSet.ReadXmlSchema(reader) line then the objDataset tells me it DOES have rows?

    I dont understand why this is happening? If the xml file is valid then why, when you pass the dataset the schema, does it not populate with data when it does populate with data without the schema??

    ARRRRRRGGGGGHHHHHH!!!!!!!!

    Wednesday, December 20, 2006 3:36 PM

Answers

  • Finally found the problem although it is still as clear as mud...

    From the top!

    Created an xmlReaderSettings object.  Given this object both the schema location AND the xml document file location.  Passed this object to the datasets readXMLSchema and then to the readxml method. 

    Now in theory, because the xmlReaderSettings object knows where both the schema AND the xml document is, it should work but it doesn't.  If I change it slightly by passing the actual xml document file name to the readxml function (rather than passing it the xmlreadersettings object) it works!

    Summary:

    Substitute the following code;

    objDataSet.ReadXml(reader)

    where reader is an instance of the xmlreadersettings class, to the code below.

    objDataSet.ReadXml(sourceURL)

    Thursday, December 21, 2006 10:00 AM

All replies

  • Reading the schema (DataSet.ReadXMLSchema) does exactly that.  It reads JUST the schema. 

    Change it to:

    XMLDataSet.ReadXml(XMLFile, XmlReadMode.ReadSchema)

    This will read the file in (the data) and use the schema in the file.  Fiddle around with the XMLReadMode items to see which one you like best.

    Wednesday, December 20, 2006 7:52 PM
  • Tried that but it made it worse.  When I call that method, I dont even get a table in the dataset!

    All the document on MSDN tells me I'm using the correct method of dataset.xmlreadschema followed by dataset.readxml.  Funny thing is we have used this elsewhere with a different schema and it works fine.  We are using an auto populating field in the schema (ID column) and hence why we have to use the schema when populating the dataset.  I know what your thinking, it must be the schema but the schema is identical in structure bar a couple of extra elements and a few name changes.

    Thursday, December 21, 2006 9:42 AM
  • Finally found the problem although it is still as clear as mud...

    From the top!

    Created an xmlReaderSettings object.  Given this object both the schema location AND the xml document file location.  Passed this object to the datasets readXMLSchema and then to the readxml method. 

    Now in theory, because the xmlReaderSettings object knows where both the schema AND the xml document is, it should work but it doesn't.  If I change it slightly by passing the actual xml document file name to the readxml function (rather than passing it the xmlreadersettings object) it works!

    Summary:

    Substitute the following code;

    objDataSet.ReadXml(reader)

    where reader is an instance of the xmlreadersettings class, to the code below.

    objDataSet.ReadXml(sourceURL)

    Thursday, December 21, 2006 10:00 AM
  • Sorry for the misunderstanding, but that's what I told you to do.  It says in my code example: XMLFILE, not XMLREADER.  I guess I wasn't clear enough.  I should've mentioned not to use the reader.  Next time I'll elaborate more so it's clearer.
    Thursday, December 21, 2006 3:17 PM