Esempio n. 1
0
        static void Main(string[] args)
        {
            dynamic element = new ExpandoObject();

            element.FirstName = "John";
            element.LastName  = "Doe";
            element.Age       = 30;

            using (StreamWriter writer = new StreamWriter("test.csv"))
            {
                writer.Write(ExpandoObjectSerializer.AsCsv(element));
            }

            ImportElementCsvFile();



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

            row           = new ExpandoObject();
            row.FirstName = "John";
            row.LastName  = "Doe";
            row.Age       = 30;
            table.AddRow(row);

            row           = new ExpandoObject();
            row.FirstName = "Jane";
            row.LastName  = "Doe";
            row.Street    = "Main street";
            table.AddRow(row);


            foreach (dynamic actualRow in table.Rows)
            {
                Console.WriteLine(
                    string.Format("{0} {1} is {2} years old.",
                                  actualRow.FirstName,
                                  actualRow.LastName,
                                  actualRow.Age));
            }


            using (StreamWriter writer = new StreamWriter("test.csv"))
            {
                writer.Write(table.AsCsv());
            }

            using (StreamWriter writer = new StreamWriter("test.xml"))
            {
                writer.Write(table.AsXml());
            }

            ImportTableCsvFile();

            Console.ReadKey();
        }
Esempio n. 2
0
        public void ExportAndImport_XML()
        {
            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);
            table.AddRow(row);

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

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

            //export
            xmlExport = table.AsXml();

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(xmlExport);
            }

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

            //import
            using (StreamReader reader = new StreamReader(fileName))
            {
                table.FromXml(ReadFile(reader));
            }

            //compare
            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);
        }