Create DataTable from XML Schema
-
Here's the deal: I have created an XSD file that relates back to an XML data file. The XML data is EMPTY (no data), only the XML Header, DataSet name and reference to the XSD file comprise the contents of the XML (see below): xmlns="http://tempuri.org/MyDataFile.xsd"> I load this XML file into a DataSet object using ReadXML() with the ReadSchema parm. While the XML data file is empty, this read statement succeeds just fine; expected this, it's all good. Now, I want to add a 'record' into the XML file, so I need to create a new DataTable, but I want to use the schema that's already defined in the XSD for my table (makes sense). In my schema (XSD) file, this record is defined as MyDataRecord (if I view the Schema in designer, I can see the datastructure just fine). Becase the XML file is empty, doing something like: DataTable dt = MyDataSet.Tables["MyDataRecord"]; only returns NULL I thought, perhaps, I could do the following: DataTable dt = MyDataSet.Tables.Add(); and it would be "smart enough" to create a DataTable object using the schema datastructure, but it does not ... I need to know how to create a DataTable object of type "MyDataRecord " so I have the complete data structure ... There has GOT to be a simple way to do this, someone please enlighten me. TIA. D.
-
Here's the deal: I have created an XSD file that relates back to an XML data file. The XML data is EMPTY (no data), only the XML Header, DataSet name and reference to the XSD file comprise the contents of the XML (see below): xmlns="http://tempuri.org/MyDataFile.xsd"> I load this XML file into a DataSet object using ReadXML() with the ReadSchema parm. While the XML data file is empty, this read statement succeeds just fine; expected this, it's all good. Now, I want to add a 'record' into the XML file, so I need to create a new DataTable, but I want to use the schema that's already defined in the XSD for my table (makes sense). In my schema (XSD) file, this record is defined as MyDataRecord (if I view the Schema in designer, I can see the datastructure just fine). Becase the XML file is empty, doing something like: DataTable dt = MyDataSet.Tables["MyDataRecord"]; only returns NULL I thought, perhaps, I could do the following: DataTable dt = MyDataSet.Tables.Add(); and it would be "smart enough" to create a DataTable object using the schema datastructure, but it does not ... I need to know how to create a DataTable object of type "MyDataRecord " so I have the complete data structure ... There has GOT to be a simple way to do this, someone please enlighten me. TIA. D.
-
Here's the deal: I have created an XSD file that relates back to an XML data file. The XML data is EMPTY (no data), only the XML Header, DataSet name and reference to the XSD file comprise the contents of the XML (see below): xmlns="http://tempuri.org/MyDataFile.xsd"> I load this XML file into a DataSet object using ReadXML() with the ReadSchema parm. While the XML data file is empty, this read statement succeeds just fine; expected this, it's all good. Now, I want to add a 'record' into the XML file, so I need to create a new DataTable, but I want to use the schema that's already defined in the XSD for my table (makes sense). In my schema (XSD) file, this record is defined as MyDataRecord (if I view the Schema in designer, I can see the datastructure just fine). Becase the XML file is empty, doing something like: DataTable dt = MyDataSet.Tables["MyDataRecord"]; only returns NULL I thought, perhaps, I could do the following: DataTable dt = MyDataSet.Tables.Add(); and it would be "smart enough" to create a DataTable object using the schema datastructure, but it does not ... I need to know how to create a DataTable object of type "MyDataRecord " so I have the complete data structure ... There has GOT to be a simple way to do this, someone please enlighten me. TIA. D.
Hi Douglas, Maybe DataAdapter.FillSchema may help you eperales
-
How do you load your Dataset?It should be like this:
XmlDataDocument doc = new XmlDataDocument();
doc.DataSet.ReadXmlSchema("schema.xsd");
doc.Load("mydataset.xml");After this you can get your tables in dataset. Mazy No sig. available now.
I create a DataSet and use ReadXML("mydatafile.xml", XmlReadMode.ReadSchema); It's loading the schema just fine, but as I stated, since the XML file is empty, essentially the DataSet contains no valid tables in it's Table Collection. So how do I go about creating a DataTable that has the datastucture defined in the schema? D.
-
Hi Douglas, Maybe DataAdapter.FillSchema may help you eperales
Ok, what you suggested did not work, since a DataAdapter in itself is an Abstract class, but it reminded me of an example I downloaded a while back that used a 'pre-canned' xml/xsd file ... The only difference between that example, and my code, is that they have you drop a DataSet object on your Window in the Form Designer window and bind it to a "typed dataset" ... which I did ... Now this seems to be working(?) I'm not sure what the difference is between creating a DataSet object programmically and attaching it to my schema and what's taking place when I do this visually ... Obviously something is being 'set' somewhere that either I am missing, or doing wrong. Perhaps someone knows the answer to this question? I'd still like to know how to make this all happen in code. D.
-
Ok, what you suggested did not work, since a DataAdapter in itself is an Abstract class, but it reminded me of an example I downloaded a while back that used a 'pre-canned' xml/xsd file ... The only difference between that example, and my code, is that they have you drop a DataSet object on your Window in the Form Designer window and bind it to a "typed dataset" ... which I did ... Now this seems to be working(?) I'm not sure what the difference is between creating a DataSet object programmically and attaching it to my schema and what's taking place when I do this visually ... Obviously something is being 'set' somewhere that either I am missing, or doing wrong. Perhaps someone knows the answer to this question? I'd still like to know how to make this all happen in code. D.
D, If you want to peek under the hood, maybe taking a look to .cs file generated by your .xsd may shed some light on the issue. On the other hand, if you want to test the FillSchema solution, use a SqlDataAdapter, I gave you the wrong link :~ eperales
-
D, If you want to peek under the hood, maybe taking a look to .cs file generated by your .xsd may shed some light on the issue. On the other hand, if you want to test the FillSchema solution, use a SqlDataAdapter, I gave you the wrong link :~ eperales
Hey, I checked the CS files in the generated code section, but I'll be #%&* if I can figure out what the differences were/are, between the generated code and what I had done programmically ... obviously there's something because it works now. Thank you for the information and taking the time to respond. D.