1

I am trying to read an XML file into a DataTable and I am having a hard time matching the schema, and converting it properly. This is what the XML could look like...

<?xml version="1.0" encoding="utf-8"?>
<School>
  <Teacher>
    <Name>Teacher1</Name>
    <Student>
      <Student-Name>Student1</Student-Name>
      <Gender>M</Gender>
      <Address>Address1</Address>
    </Student>
    <Student>
      <Student-Name>Student2</Student-Name>
      <Gender>F</Gender>
      <Address>Address2</Address>
    </Student>
  </Teacher>
  <Teacher>
    <Name>Teacher2</Name>
    <Student>
      <Student-Name>Student3</Student-Name>
      <Gender>F</Gender>
      <Address>Address3</Address>
    </Student>
  </Teacher>
</School>

I need to add it to a DataTable so that it the table looks like this

Name           Student-Name    Gender    Address
----------------------------------------------------
Teacher1    Student1             M    Address1
Teacher1    Student2             F    Address2
Teacher2    Student3             F    Address3

DataTable.ReadXML() --> wont allow me to read in this XML file. The error says that DataTable doesn't support schema inference
DataSet.ReadXML() --> doesn't pick up on the schema and separates this into two tables the first being a Teacher Table and the second being a Student Table, even when it is set to Infer the schema.

The other problem is that I am doing this with several types of XML files. All of which have a different number of nodes nested within the student, and I need to make this generic so that it can read the XML regardless of the number of nodes.

I am wondering if I need to set up an XmlReader to read this in, or if there is any settings on the DataSet.ReadXML()/DataTable.ReadXML() that I need to use. Thank You.

2 Answers 2

0

If you are familiar with Xsd.exe (a tool in visual studio), you can generate Classes from the XML data which will allow you to load the data, in bulk, directly from the XML into the generated Classes.

1
  • Thanks. I ended up just merging the two tables that were created by the DataSet.ReadXml() because they are assigned an extra value that relates the nested nodes to the parent node.
    – JBlaze
    Jul 2, 2013 at 13:37
0

You encounter this problem because you are working using a DataTable (not a DataSet) and your XML schema has not been defined.

Here is an example in which SQL data are exported in a DataTable

Dim sTable As String = "payement"
Dim sFileName = "db.select." & sTable & ".xml"
Dim cmd = New NpgsqlCommand(sSQL, npgconx)
Dim rxSheet as IDataReader= cmd.ExecuteReader()

Dim dtSheet = New DataTable(sTable)
dtSheet.Load(rxSheet)
dtSheet.WriteXml(sFileName)
dtSheet.WriteXmlSchema(sFileName.Replace(".xml", ".Schema.xml"))

In this code just after having exported SQL Data in XML file, WriteXmlSchema() function is used to export XML Schema.

and now, same data are imported in a DataTable

Dim sTable As String = "payement"
Dim sFileName = "db.select." & sTable & ".xml"
Dim dtSheet = New DataTable(sTable)
dtSheet.ReadXmlSchema(sFileName.Replace(".xml", ".Schema.xml"))
dtSheet.ReadXml(sFileName)

It is important that ReadXmlSchema() function is executed before ReadXml() function.

In WriteXml() function it is also important to pass the table's name.

I use this code to serialize tuples returned by a SELECT SQL command on my PC when number of records is extremely high so next time I want to test my program I don't load DataBase with my big extraction ... and some time I don't wait a lot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.