DynamicTable to XML

This DynamicTable object is a dynamic and expandable data class to store a table. You will find an introduction to this data class in a previous article. In the newest version of the DynamicTable I have implemented XML serialization functions to import and export a DynamicTable from or to an XML file. You will find the release on github and you are welcome to use the DynamicTable in your own projects.


DynamicTable to XML

The following source code shows a unit test function. Within this unit test a DynamicTable will be written to a XML file and a new DynamicTable will be created by reading the file.

IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
dynamic row;
string xmlExport;

string fileName = _assemblyDirectory + @"\CsvTest.xml";

//add values
row = new ExpandoObject();
row.FirstName = "Hans";
row.LastName = "Mueller";
row.Age = 30;
row.TimeStamp = new DateTime(2012, 12, 24, 1, 2, 3);

row = new ExpandoObject();
row.LastName = "Meier";
row.Street = "Main street";

Assert.AreEqual(2, table.Rows.Count);
Assert.AreEqual(5, table.Columns.Count);

xmlExport = table.AsXml();

using (StreamWriter writer = new StreamWriter(fileName))

//remove rows
Assert.AreEqual(0, table.Rows.Count);
Assert.AreEqual(5, table.Columns.Count);

using (StreamReader reader = new StreamReader(fileName))

row = table.Rows[0];
Assert.AreEqual("Hans", row.FirstName);
Assert.AreEqual("Mueller", row.LastName);
Assert.AreEqual(30, row.Age);

Assert.AreEqual(2012, row.TimeStamp.Year);
Assert.AreEqual(12, row.TimeStamp.Month);
Assert.AreEqual(24, row.TimeStamp.Day);
Assert.AreEqual(1, row.TimeStamp.Hour);
Assert.AreEqual(2, row.TimeStamp.Minute);
Assert.AreEqual(3, row.TimeStamp.Second);

Assert.AreEqual("", row.Street);

row = table.Rows[1];
Assert.AreEqual("", row.FirstName);
Assert.AreEqual("Meier", row.LastName);
Assert.AreEqual(0, row.Age);
Assert.AreEqual(0, row.TimeStamp.Ticks);
Assert.AreEqual("Main street", row.Street);

To read a file the following function is used.

private static IEnumerable<string> ReadFile(StreamReader reader)
    while (reader.EndOfStream == false)
        yield return reader.ReadLine();


The DynamicTable object offers a flexible way to manage data within a table. By using the XML serialization functions it is possible to store the content of the DynamicTable into a file.

Dieser Beitrag wurde unter .NET, C#, DynamicObjects abgelegt und mit , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Eine Antwort zu DynamicTable to XML

  1. Nicolas schreibt:

    Here’s a real story. We worked on a precjot that was pretty well tested. Still, one day we discovered that one of the getter methods fails with a null-pointer-exception if its instance was created via the default (no-arg) constructor. It is a something that our tests didn’t cover.One of our team members decided to overcome this lack of testing (as he called it) by writing a test that goes over all classes in our precjot, instantiates each one and invokes every getter method. This, of course, made sure that the problem will not repeat it self. This approach is not as good as it sounds. Although it raised the coverage of most getter methods to 100% it did not check the return values. We were no longer able to use coverage to detect the methods that had no real test. Eventually, we removed this artificial test from the suite.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:


Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s