Example #1
0
        public static void WriteToDataset()
        {
            Console.WriteLine(CultureInfo.CurrentCulture);
            Console.WriteLine();
            Console.WriteLine();
            DataSet dataSet = new DataSet();

            dataSet.Locale = new CultureInfo("de-DE");

            DataTable dataTable = new DataTable("abcd");

            dataSet.Tables.Add(dataTable);
            dataTable.Columns.Add(new DataColumn("Col1", typeof(string)));
            dataTable.Rows.Add(100000000.12345678911);
            //dataTable.Rows.Add("2,5");
            dataTable.Rows.Add(DBNull.Value);
            //dataTable.AcceptChanges();

            dataSet.AcceptChanges();

            dataSet.WriteXml("d:\\temp\\myfile21431.txt", XmlWriteMode.WriteSchema);

            Console.Write(dataSet.GetXml());
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("***************Schema*************");
            Console.WriteLine();
            Console.Write(dataSet.GetXmlSchema());


            DataSet dataSet1 = new DataSet();

            dataSet1.ReadXml("d:\\temp\\myfile21431.txt");

            Console.WriteLine();
            Console.WriteLine();
            Console.Write(dataSet1.GetXml());
        }
        public static void Main()
        {
            string    xml     = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
        <DATAPACKET Version=""2.0"">
            <METADATA>
                <FIELDS>
                    <FIELD attrname=""CompanyID"" fieldtype=""string"" WIDTH=""10""/>
                    <FIELD attrname=""Description"" fieldtype=""string"" WIDTH=""40""/>
                </FIELDS>
                <PARAMS/>
            </METADATA>
            <ROWDATA>
                <ROW CompanyID=""CC"" Description=""Contoso""/>
            </ROWDATA>
        </DATAPACKET>
        ";
            XmlReader reader  = XmlReader.Create(new StringReader(xml));
            DataSet   dataSet = new DataSet();

            dataSet.ReadXml(reader, XmlReadMode.InferTypedSchema);
            var rowTable = dataSet.Tables["ROW"];
            var newRow   = rowTable.NewRow();

            newRow["CompanyID"]   = "APPL";
            newRow["Description"] = "Apple";
            rowTable.Rows.Add(newRow);
            Console.WriteLine(dataSet.GetXml());


            XDocument doc       = XDocument.Parse(xml);
            DataTable rowTable2 = new DataTable("Table1");
            DataRow   newRow2   = null;

            foreach (XElement field in doc.Descendants("FIELD"))
            {
                string t     = (string)field.Attribute("fieldtype");
                Type   _type = null;
                switch (t)
                {
                case "string":
                    _type = typeof(string);
                    break;
                }

                rowTable2.Columns.Add((string)field.Attribute("attrname"), _type);
            }
            foreach (XElement row in doc.Descendants("ROW"))
            {
                newRow = rowTable2.Rows.Add();
                foreach (XAttribute attribute in row.Attributes())
                {
                    newRow[attribute.Name.LocalName] = (string)attribute;
                }
            }
            newRow = rowTable2.Rows.Add();
            newRow["CompanyID"]   = "APPL";
            newRow["Description"] = "Apple";
            DataSet ds = new DataSet();

            ds.Tables.Add(rowTable2);
            Console.WriteLine(ds.GetXml());
        }