DataSet WriteXML and XMLValidatingReader

A

Axe

Sorry about the crossposting but I think my question validly fits in both
categories and insight from experts would be extremely useful.

I populate a dataset from my database. I then create an XML file using the
dataset's WriteXml() method.

In a seperate application, I'd like to read that XML file back into a
dataset but before I do this I'd like to validate it to ensure that the
elements fit a predefined XSD schema.

I know I can use the XmlValidatingReader for this but my XML file references
no schema. The framework just created an XML file with a root element
(NewDataSet) with no attributes and then each table is a child element
(Table, Table1...), etc.

So my question is this....do I have to programatically add the namespace and
schema attributes to the root element or is there some way I can use
WriteXML() to include the related schema? I do not, however, wish to
include an inline schema since this does not validate properly as the schema
is a child element to the root element.

Bottom line: How can I use the XmlValidatingReader to validate an XML file
generated from a dataset's WriteXml method?

Axe
 
D

Dan Normington

If you specify the XmlWriteMode you can specify:
myDataSet.WriteXml("c:\\temp.xml",System.Data.XmlWriteMode.WriteSchema);

However, WriteSchema is supposed to be the default. I'm curious as to why it
isn't writing the schema?

If you want the schema seperate you can call FillSchema on the dataadapter
object. This should setup constraints, and primary keys for you.

I'm going to look into it a bit more. I'll get back to you if somebody
hasn't give the answer.
 
A

Axe

Dan,

My XML document is very simple and has no constraints or relationships
defined. And I've used the XmlWriteMode parameter which just puts the
schema inline. That, however, wasn't able to be validated using the
XmlValidatingReader.

I've taken the dataset generated XmlSchema and made it a standalone XSD
file. Now I need to somehow run that XSD file and the XML file in through
the XmlValidatingReader. My problem is that the dataset generated XML file
does not reference any XSD file. That's the problem.

The examples in the SDK's show XML documents referencing XSD files.

After I generate the XML file I could change the <DataSet> root element and
add the proper attributes using XmlDocument or such. Is this the only way?
 
A

Axe

To put some closure to this posting, I've found (after an exhaustive search)
a site that yielded some answers to my questions and proved to solve my
problem:

http://www.daveandal.net/books/4923/updating-data/load-dataset-validate.aspx?xmlfile=somebooks.xml

And click on the "view source" link to see their aspx code which uses an
XmlValidatingReader in the way I needed it. You can also view their XML and
XSD file which seem to have been generated from a dataset as was mine.

The bottome line here was to use the XmlSchemaCollection and add my schema
to the collection, then add the collection to my XmlValidatingReader.

Axe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top