DataTable.ReadXML Schema inference error


I want to populate 2 tables from 2 xml files one for states and one for countries that look like this:

<?xml version="1.0" encoding="utf-8" ?>
<States>
  <State>
    <Code>AK</Code>
    <Name>ALASKA</Name>
  </State>
  <State>
    <Code>AL</Code>
    <Name>ALABAMA</Name>
  </State>
</States>


I try to create a table from it with this in the constructor of a provider ob:

            postalCodes = new DataSet("PostalCodes");

            XmlTextReader sStreamer = new XmlTextReader(statesPath);
            XmlTextReader cStreamer = new XmlTextReader(countriesPath);

            DataTable states = new DataTable("States");
            DataTable countries = new DataTable("Countries");

            states.Locale = System.Globalization.CultureInfo.CurrentCulture;
            countries.Locale = System.Globalization.CultureInfo.CurrentCulture;

            countries.ReadXml(cStreamer);
            states.ReadXml(sStreamer);
           
            cStreamer.Close();
            sStreamer.Close();

            postalCodes.Tables.Add(countries);
            postalCodes.AcceptChanges();

            postalCodes.Tables.Add(states);
            postalCodes.AcceptChanges();

           

But I get an error...

System.InvalidOperationException : DataTable does not support schema inference from Xml.

MSDN suggests using an overloaded method that supports XMLReadMode but this functionality is apparently hidden.

From MSDN: http://msdn2.microsoft.com/en-us/library/z447ey0x.aspx
"The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema. To read both data and schema, use one of the ReadXML overloads that includes the XmlReadMode parameter, and set its value to ReadSchema."


I also noticed this (same article):
"If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. If the schema cannot be extended through inference in order to expose all data, an exception is raised."

Question: How do you get it to infer the structure?
Sub Question: Is there a better way to approach loading these tables in the dataset?

Urgent.
MattCoatneyAsked:
Who is Participating?
 
anarki_jimbelConnect With a Mentor Commented:
Hmmm....

Not basic task, I'd say. No bloody info.

OK, I did a trick again.

I have created a schema file for two tables. That was easy really.
Now I have data in two separate xml files.

So first I read schema then data:

            DataSet ds = new DataSet();
           
            ds.ReadXmlSchema("postcodes.xsd");

            ds.Tables["Country"].ReadXml("countries.xml");
            ds.Tables["State"].ReadXml("states.xml");
           
            dataGridView1.DataSource = ds.Tables[1];      
           
            foreach(DataTable dt in ds.Tables)
            {
                listBox1.Items.Add(dt.TableName);
            }

It's working.

I created schema once by loading my 'postcodes.xm' (see previous post) and saving it by ds.WriteXmlSchema
to "postcodes.xsd". Since that I reuse it to load the schema.
After I load it my tables are ready for data. Easy. I think it's not too messy to have a schema for entire dataset.

Does this help?
0
 
anarki_jimbelCommented:
Have no much time at the moment but may be it will help:

I created a "joint" xml:

<?xml version="1.0" encoding="utf-8" ?>
<postalcodes>
    <Country>
      <Name>US</Name>
    </Country>
    <Country>
      <Name>Canada</Name>
    </Country>
    <State>
      <Code>AK</Code>
      <Name>ALASKA</Name>
    </State>
    <State>
      <Code>AL</Code>
      <Name>ALABAMA</Name>
    </State>
</postalcodes>

It is valid one, no worries. It has two tables: State and Country.
You can read it into DataSet:

            DataSet ds = new DataSet();

            ds.ReadXml("postcodes.xml");


            dataGridView1.DataSource = ds.Tables[1];
           
            foreach(DataTable dt in ds.Tables)
            {
                listBox1.Items.Add(dt.TableName);
            }

As u can see I have datagrid and listbox.
So I can display any of two tables and I can see list of tables in the listbox.

Let me know if this approach not satisfying one...
0
 
MattCoatneyAuthor Commented:
Although this method does work, it leaves me with a messier set of data. I'm going to leave this one open a while longer in the hope that someone knows how I can do this and still keep 2 files. Otherwise I am very pleased with your response.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
anarki_jimbelCommented:
By the way, my xml files:

<?xml version="1.0" encoding="utf-8"?>
<States>
      <State>
            <Code>AK</Code>
            <Name>ALASKA</Name>
      </State>
      <State>
            <Code>AL</Code>
            <Name>ALABAMA</Name>
      </State>
</States>

and

<?xml version="1.0" encoding="utf-8"?>
<Countries>
      <Country>
            <Name>US</Name>
      </Country>
      <Country>
            <Name>Canada</Name>
      </Country>
</Countries>
0
 
MattCoatneyAuthor Commented:
You have earned your points. I still wish I didn't have to get the schema that way, but that is not your fault.

Thank you
0
 
anarki_jimbelCommented:
I don't much care about points. Just curiosity :)

How do you want the schema? I just have no time currently to play with this stuff.
In any case you need a schema (the problem with the exception you have is reletaed to the absence of schema).
So you need some schema. How do you get it is another story.
You may create it in any special application like XMLSPY.

I just used the quick and dirty way.
To make it properly you mayto create tables structure manually in your code and impose all restrictions like datatypes etc. And try to load data into tables created in this way.

or you may create table structure (columns etc) and try to load schema for each table separately (it seems it's imposible to load schema to an "empty", not structured table.). Not sure if this approach will work however.

There is a lot of other tricks you can do. E.g., ou may read both documents into memory and combine them into one XmlDocument, and load the document into resultset, it will create tables and you dont need schema and still have two separate data files.


And so on :)
0
 
MattCoatneyAuthor Commented:
Well you answered my question.

I considered similar techniques of getting the schema before I posted, but I was hoping someone knew how to get the Table.ReadXml to infer the schema like the parent DataSet.ReadXml does.

So I am using your method and it works just fine. An extra file isn't too bad I suppose. I'm not at all unsatisfied with your efforts, but I realize sometimes we ask questions and the answer is "You can't do it that way, here is another way"
0
 
anarki_jimbelCommented:
It was just interesting. I prefer to answer questions than blabbing at stupid chats :)

I deal with xml all the time but not with resultsets etc.
However this ADO technique is worth to play with (and sometimes I need it). I couldn't find exact answer to your question that's why...
It's interesting to try to create a table with proper structure (from code) and try to load xml. Will do later.

However all that 's happening makes sense. Even when working with xml like XmlDocument etc it's a good idea (and good practice) to have xml schema even for very simple xml documents.

Any way, good luck! :)
0
All Courses

From novice to tech pro — start learning today.