none
Serializing fields in a custom DataSet RRS feed

  • Question

  • Is there any way to add fields to a DataSet which will be serialized along with the table information when the DataSet is serialized in XML? I have some information that describes the DataSet that I want to try to include inside it when I throw it around in XML. I've tried adding fields and classes in the partial class Visual Studio generates for you for validation methods but I can't seem to get them to show up alongside the table information in the XML file either when myDataSet.WriteXML() or I use an XMLSerializer.
    Thursday, September 10, 2009 11:48 PM

Answers

  • The only mechanism is ExtendedProperties. Either that or you could add additional DataColumns to tables, or perhaps you could add another DataTable to keep this information in the DataSet as well

    Thanks
    Chris Robinson
    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Yichun_Feng Thursday, September 17, 2009 2:29 AM
    Monday, September 14, 2009 4:11 PM

All replies

  • Hi Huge,

    Do you want to add columns with DataTables of a DataSet? Can you give us some more details reg. this, so that we can help you!


    baskaran r
    Friday, September 11, 2009 6:01 AM
  • Obviously I do not have all your requierments, but it sounds like you want to add some descriptive data to the written xml.
    Since this is XML, why not just add this information into a XML comment into the output.

                // Some tables to put into the dataset
                DataTable dt1 = new DataTable("TableOne");
                DataTable dt2 = new DataTable("TableTwo");
                dt1.Columns.Add(new DataColumn("T1_C1", typeof(string)));
                dt2.Columns.Add(new DataColumn("T2_C1", typeof(string)));
                dt1.Rows.Add(new object[] { "Row in Table One" });
                dt2.Rows.Add(new object[] { "Row in Table Two" });
                DataSet ds = new DataSet();
                ds.Tables.Add(dt1);
                ds.Tables.Add(dt2);
    
                // Do the actual writing to XML
                String xmlFile = @"C:\Temp\xmlOut.xml";
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(xmlFile, settings))
                {
                    writer.WriteComment("This DataSet contains some tables and rows.");
                    ds.WriteXml(writer);
                }
    This will generate the following output:

    <?xml version="1.0" encoding="utf-8"?>
    <!--This DataSet contains some tables and rows.-->
    <NewDataSet>
      <TableOne>
        <T1_C1>Row in Table One</T1_C1>
      </TableOne>
      <TableTwo>
        <T2_C1>Row in Table Two</T2_C1>
      </TableTwo>
    </NewDataSet>
    


    HTH
    //Michael

    This posting is provided "AS IS" with no warranties.
    Friday, September 11, 2009 8:08 AM
  • Sorry, I guess I was a little sparse with the details.

    I want to store inside the DataSet some integers, booleans, maybe even a separate class, etc. that help determine what kinds of calculations to run on the data in the tables within the DataSet. The information isn't really suitable for its own DataTable, so I'm looking for a way to include it within the derived DataSet that Visual Studio generates for you so that the information will be serialized in XML along with the rest of the DataSet, kind of like this:

    <DataSet1>
        ... lots of stuff on tables and whatnot in here
    </DataSet1>
    <MyInfo>
        ... integers, booleans, doubles, whatever need be, just separate from DataSet1
    </MyInfo>

    Where MyInfo is maybe a class with the information I need. It sits outside of DataSet1's tags in the above but I don't care where it sits as long as it can be read back in successfully.


    After thinking about it some more I realized I could get pretty much the same functionality from packaging the DataSet in another class itself and just including what I need in that one, kind of like this:

    public class MyContainerClassForADataSetAndSomeOtherStuff {
    
    int hey;
    int you;
    boolean isThereAnybodyOutThere;
    MyType aWholeClassOfOtherGoodies;
    
    DataSet1 heresMyDataSet;
    }

    And then just serializing MyContainerClassForADataSetAndSomeOtherStuff into XML, where it would successfully serialize everything and then the DataSet1 alongside it all. However, I'd really like to keep it all in the DataSet if possible.

    After asking around I found the ExtendedProperties property of the DataSet which allows you to store Strings in with the DataSet that will be serialized in XML along with it, but this is a little clunky for what I'd like to do.

    Friday, September 11, 2009 1:18 PM
  • The only mechanism is ExtendedProperties. Either that or you could add additional DataColumns to tables, or perhaps you could add another DataTable to keep this information in the DataSet as well

    Thanks
    Chris Robinson
    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Yichun_Feng Thursday, September 17, 2009 2:29 AM
    Monday, September 14, 2009 4:11 PM