none
How to save a data table to an xmlDocument and return as a string and then recreate the datatable from the string? RRS feed

  • Question

  • I havent worked with strings or xml before, I need to take a datatable and return as a string and then recreate the dt from the string, This is all I have so far:

            DataTable dt = ds.Tables[0];
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
            ds.WriteXml(xmlTextWriter, XmlWriteMode.IgnoreSchema);
            string contentAsXmlString = stringWriter.ToString();

    Thank you for any help or suggestions!
    McC
    Tuesday, October 20, 2009 11:44 AM

Answers

  • Not sure what exactly you are trying to do, or what errors you are getting (if any). This should work though:

                DataTable dt = new DataTable("TheTable");
                dt.Columns.Add(new DataColumn("id", typeof(int)));
                dt.Columns.Add(new DataColumn("fname", typeof(string)));
                dt.Columns.Add(new DataColumn("lname", typeof(string)));
                dt.Rows.Add(new object[] { 1, "John", "Johnson" });
                dt.Rows.Add(new object[] { 2, "Paul", "Paulson" });
                dt.Rows.Add(new object[] { 3, "Mary", "Maryson" });
    
                TextWriter tw = new StringWriter();
                dt.WriteXml(tw, XmlWriteMode.WriteSchema);
    
                Console.WriteLine(tw.ToString());
    
                DataTable newTable = new DataTable();
                TextReader sr = new StringReader(tw.ToString());
    
                newTable.ReadXml(sr);

    You need to add the schema, otherwise you will get an exception:

    System.InvalidOperationException was unhandled
    Message="DataTable does not support schema inference from Xml."

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    • Marked as answer by Yichun_Feng Monday, October 26, 2009 6:37 AM
    Tuesday, October 20, 2009 1:08 PM
  • Depending on what you're doing, you could use the DataSet's built in methods (IOW, if you have a DataSet with only one table, then you might as well save yourself a lot of trouble):

    So, to get an XML string of data from a DataSet, simply use:

    string xml = MyDataSet.GetXml();

    Then, to reserialize the XML back into the DataSet, do this:

    StringReader sr = new StringReader(xml);
    MyDataSet.ReadXml(sr, XmlReadMode.InferSchema);
    // you may also want to add this, depending on what your needs are
    MyDataSet.AcceptChanges();

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Edited by BonnieBMVP Wednesday, October 21, 2009 5:34 PM bad formatting
    • Marked as answer by Yichun_Feng Monday, October 26, 2009 6:37 AM
    Wednesday, October 21, 2009 5:34 PM

All replies

  • Not sure what exactly you are trying to do, or what errors you are getting (if any). This should work though:

                DataTable dt = new DataTable("TheTable");
                dt.Columns.Add(new DataColumn("id", typeof(int)));
                dt.Columns.Add(new DataColumn("fname", typeof(string)));
                dt.Columns.Add(new DataColumn("lname", typeof(string)));
                dt.Rows.Add(new object[] { 1, "John", "Johnson" });
                dt.Rows.Add(new object[] { 2, "Paul", "Paulson" });
                dt.Rows.Add(new object[] { 3, "Mary", "Maryson" });
    
                TextWriter tw = new StringWriter();
                dt.WriteXml(tw, XmlWriteMode.WriteSchema);
    
                Console.WriteLine(tw.ToString());
    
                DataTable newTable = new DataTable();
                TextReader sr = new StringReader(tw.ToString());
    
                newTable.ReadXml(sr);

    You need to add the schema, otherwise you will get an exception:

    System.InvalidOperationException was unhandled
    Message="DataTable does not support schema inference from Xml."

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    • Marked as answer by Yichun_Feng Monday, October 26, 2009 6:37 AM
    Tuesday, October 20, 2009 1:08 PM
  • Depending on what you're doing, you could use the DataSet's built in methods (IOW, if you have a DataSet with only one table, then you might as well save yourself a lot of trouble):

    So, to get an XML string of data from a DataSet, simply use:

    string xml = MyDataSet.GetXml();

    Then, to reserialize the XML back into the DataSet, do this:

    StringReader sr = new StringReader(xml);
    MyDataSet.ReadXml(sr, XmlReadMode.InferSchema);
    // you may also want to add this, depending on what your needs are
    MyDataSet.AcceptChanges();

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Edited by BonnieBMVP Wednesday, October 21, 2009 5:34 PM bad formatting
    • Marked as answer by Yichun_Feng Monday, October 26, 2009 6:37 AM
    Wednesday, October 21, 2009 5:34 PM