Solved

DataTable.ReadXML Schema inference error

Posted on 2006-10-27
8
12,782 Views
Last Modified: 2013-11-19

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.
0
Comment
Question by:MattCoatney
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17837804
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
 

Author Comment

by:MattCoatney
ID: 17844691
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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 17847635
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17847642
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:MattCoatney
ID: 17851298
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17852812
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
 

Author Comment

by:MattCoatney
ID: 17852964
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17854584
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now