Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Create DataTable from XML Schema

Create DataTable from XML Schema

Scheduled Pinned Locked Moved C#
xmldatabasetutorial
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Douglas Troy
    wrote on last edited by
    #1

    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.

    M E 2 Replies Last reply
    0
    • D Douglas Troy

      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.

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      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.

      D 1 Reply Last reply
      0
      • D Douglas Troy

        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.

        E Offline
        E Offline
        Ernesto Perales Soto
        wrote on last edited by
        #3

        Hi Douglas, Maybe DataAdapter.FillSchema may help you eperales

        D 1 Reply Last reply
        0
        • M Mazdak

          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.

          D Offline
          D Offline
          Douglas Troy
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • E Ernesto Perales Soto

            Hi Douglas, Maybe DataAdapter.FillSchema may help you eperales

            D Offline
            D Offline
            Douglas Troy
            wrote on last edited by
            #5

            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.

            E 1 Reply Last reply
            0
            • D Douglas Troy

              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.

              E Offline
              E Offline
              Ernesto Perales Soto
              wrote on last edited by
              #6

              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 1 Reply Last reply
              0
              • E Ernesto Perales Soto

                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 Offline
                D Offline
                Douglas Troy
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups