Create XML file using existing XSD schema
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

