MattCoatney
asked on
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(countriesPat
DataTable states = new DataTable("States");
DataTable countries = new DataTable("Countries");
states.Locale = System.Globalization.Cultu
countries.Locale = System.Globalization.Cultu
countries.ReadXml(cStreame
states.ReadXml(sStreamer);
cStreamer.Close();
sStreamer.Close();
postalCodes.Tables.Add(cou
postalCodes.AcceptChanges(
postalCodes.Tables.Add(sta
postalCodes.AcceptChanges(
But I get an error...
System.InvalidOperationExc
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.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<?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>
ASKER
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
Thank you
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 :)
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 :)
ASKER
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"
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"
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! :)
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! :)
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.Tabl
}
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...