将字符串中的数据插入DataTable

时间:2015-05-13 21:54:26

标签: c# xml datatable

我有这个DataTable:

Option

我有一个从XML文件中获取的字符串列表,如下所示:

DataTable callsDT = new DataTable();
callsDT.Columns.Add("id", typeof(Int32));
callsDT.Columns.Add("origination", typeof(String));
callsDT.Columns.Add("start_time", typeof(DateTime));
callsDT.Columns.Add("from", typeof(String));
callsDT.Columns.Add("from_name", typeof(String));
callsDT.Columns.Add("from_number", typeof(Int32));

我需要迭代每个字符串并将每个数据点分配给一个变量,但我很难想出一个优雅的方法。

1 个答案:

答案 0 :(得分:0)

使用ReadXML和WriteXML方法更容易,如下面的代码。您需要将DataTable放入DataSet才能使用此方法。您缺少输入字符串中的一列(来自名称),最后一个int32需要很长。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME1 = @"C:\temp\test.xml";

        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            DataTable callsDT = new DataTable();
            ds.Tables.Add(callsDT);
            callsDT.Columns.Add("id", typeof(Int32));
            callsDT.Columns.Add("origination", typeof(String));
            callsDT.Columns.Add("start_time", typeof(DateTime));
            callsDT.Columns.Add("from", typeof(String));
            callsDT.Columns.Add("from_name", typeof(String));
            callsDT.Columns.Add("from_number", typeof(long));

            List<List<object>> input = new List<List<object>>(){
               new List<object>() {55555, "incoming", DateTime.Parse("2001-01-01 00:00:00"), "John",  "Doe", 5555555555},
               new List<object>() {55556, "incoming", DateTime.Parse("2001-01-02 00:00:00"), "Mary",  "Smith", 5555555556},
               new List<object>() {55557, "incoming", DateTime.Parse("2001-01-03 00:00:00"), "Rob",  "Robon", 55555555557},
               new List<object>() {55558, "incoming", DateTime.Parse("2001-01-04 00:00:00"), "Sally",  "Rode", 5555555558}
            };

            foreach(List<object> newRow in input)
            {
                callsDT.Rows.Add(newRow.ToArray());
            }

            ds.WriteXml(FILENAME1, XmlWriteMode.WriteSchema);

            ds = null;
            ds = new DataSet();
            ds.ReadXml(FILENAME1, XmlReadMode.ReadSchema);


        }
    }
}
​
相关问题