将字符串中的数据插入DataTable

时间:2021-08-05 20:20:43

I have this DataTable:

我有这个DataTable:

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));

I have a list of strings taken from an XML file that look like this:

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

  <call id="55555" origination="incoming" start_time="2001-01-01 00:00:00" from_name="John Doe" from_number="5555555555 </call>

I need to iterate through each string and assign each data point to a variable but am struggling to come up with an elegant way of doing that.

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

1 个解决方案

#1


0  

Much easier to use ReadXML and WriteXML methods like code below. You need to put DataTable into a DataSet to be able to use this method. You are missing one column in your input string (from name) and you last int32 needs to be a long.

更容易使用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);


        }
    }
}
​

#1


0  

Much easier to use ReadXML and WriteXML methods like code below. You need to put DataTable into a DataSet to be able to use this method. You are missing one column in your input string (from name) and you last int32 needs to be a long.

更容易使用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);


        }
    }
}
​