Create XML file using existing XSD schema

What is the best approach to use to create an XML file from data from a database that will match an existing XSD schema file?
I have tried multiple ways, but none have accomplished everything that I have needed.

I am using VB.Net and developing in Compact Framework for handhelds.

I want the XML to match the following layout.

<?xml version="1.0" encoding="UTF-8"?>
<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://location/cars.xsd">
<PROJECT_TABLE1>
<ID>11870</ID>
<additional data....>
<PROJECTS_TABLE2>
<ID>11870</ID>
<SEQ_NO>1001</SEQ_NO>
<additional data....>
<PROJECTS_TABLE3>
<SEQ_NO>1001</SEQ_NO>
<PROJ3_SEQ_NO>1101</PROJ3_SEQ_NO>
<additional data....>
</PROJECT_TABLE3>
</PROJECT_TABLE2>
</PROJECT_TABLE1>
</PROJECTS>

The data that is populated in the xml file will come from three different database tables.
I have been able to create an xml file that is very similar to the above xml file, but it didn't include the namespace information (xmlns:xsi & xsi:noNameSpaceSchemaLocation).

One of the things that I tried was creating a dataset. I then used ReadXmlSchema since it would read the specified schema from the file into the dataset. I have gotten this to work successfully, but once I get the schema loaded how do I load data from the database into the dataset using the schema I have just loaded? Is this the proper approach or is there a better way to accomplish this task?

I have also tried adding the datatables individually to the dataset and created relationships among the three tables. The code is similar to the following:

'Retrieve all Table1 Records into the Dataset
daTable1.SelectCommand.Connection = objSqlCon
daTable1.Fill(dsDataset,"TABLE1")
'Retrieve Table2 Records for Transfer
daTable2.SelectCommand.Connection = objSqlCon
daTable2.Fill(dsDataset,"TABLE2")

' relate TABLE1 and TABLE2 in a Nested Fashion
Dim ProjRel As DataRelation
ProjRel = New DataRelation("ProjRelation", _
dsDataset.Tables("TABLE1").Columns("ID"), _
dsDataset.Tables("TABLE2").Columns("ID"))
ProjRel.Nested = True
dsDataset.Relations.Add(ProjRel)

dsDataset.WriteXml("output.xml")
This created the xml in the fashion I needed, but didn't add the namespace info.
This also was a manual process that didn't use the existing schema.

I have also tried to create the XML in this way.

dsDataSet..ReadXmlSchema("\My Documents\PROJECTS.xsd")
Using a dataadapter for the tables, I loaded the data into datatables and looped through the rows and did the following:

Dim drRow As DataRow
Dim i As Integer

drRow = dsDataset.Tables("TABLE1").NewRow()
daTable1.SelectCommand.Connection = objSqlCon
daTable1.Fill(dtTable1)
For i = 0 To (dtTable1.Rows.Count - 1)
drRow = dtTable1.Rows(i)
dsDataset.Tables("TABLE1").ImportRow(drRow)
drRow = Nothing
Next

I also did the same thing for the second table. The only problem is that by using the ImportRow command, I believe that the schema of the row from dtTable1 is copied across with the data.

Any help or ideas will be greatly appreciated.
Thanks,
Carey
[3596 byte] By [careysc] at [2007-11-19 3:40:46]
# 1 Re: Create XML file using existing XSD schema
To the best of my knowledge there is no way to create XML files from a schema, at least not automatically, there are XML editors the impose restrictions on the user based on a schema, But I guess that's not really relevant for your case. Admittedly I don't know much about schemas, I have always considered schemas to be next to useless, and a complete waste of time.

Personally I would extract the data from the database to whatever XML format that suits the extraction mechanism best. And then use an XSL Transformation on the XML file to get to the format I wanted.
khp at 2007-11-10 3:27:50 >
# 2 Re: Create XML file using existing XSD schema
Thanks for the information.

I have done a little with XSL, but that was through ASP to create a web page. I haven't done anything with XSL to modify an XML page. Can you point me to some reference material or some sample code that could help me accomplish that?

Thanks Again,
Carey
careysc at 2007-11-10 3:28:51 >
# 3 Re: Create XML file using existing XSD schema
Creating XML using XSLT is rather simmilar to creating HTML, except that you are not restricted to using html tags. Of course you must not set the output method to html or text.

And of course you will need a transformation engine you can call either via a commandline or from a program. I prefer Xalan-J http://xml.apache.org , but there are many other options.

You might want to checkout my post in this thread http://www.dev-archive.com/forum/showthread.php?t=318662 In which I explain how to setup a stylesheet, with a default template that copies element from the source document and how to create more specialized templates.

As for references I always find http://www.w3.org/TR/xslt extremely usefull.
khp at 2007-11-10 3:29:49 >