Solved

DataTable.ReadXML Schema inference error

Posted on 2006-10-27
8
12,977 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 30

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 30

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 30

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
 

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 30

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 30

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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