打印具有自定义行的数据表

时间:2014-11-20 20:08:12

标签: c# linq datatable linq-to-xml

这是我的代码

List<Customer> customersList =
                (
                    from e in XDocument.Load(file).Root.Elements("cust")
                    select new Customer
                    {
                        CustomerID = (int)e.Attribute("custid"),
                        FirstName = (string)e.Attribute("fname"),
                        LastName = (string)e.Attribute("lname"),
                        ShowsNumber = (int)e.Attribute("count_noshow"),
                        VisitNumber = (int)e.Attribute("count_resos"),
                        Cancellation = (int)e.Attribute("count_cancel"),

                    }).ToList();
                DataTable dataTable = getBasicDataTable();
                for (int i = 0; i < customersList.Count; i++)
                {
                    dataTable.Rows.Add(customersList[i]);

                }

                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Console.WriteLine(dataTable.Rows[i]["customerID"]);
                }

如您所见,我将列表添加到我的数据表中,现在我正在尝试在数据表行中打印customerID列的值。但控制台打印出这个:

ImporterOpenTable.Customer这是自定义类Customer,但我正在寻找值。你能帮帮我吗

UPDATE1

为了给你全部图片,这是我的getBasicDataTable函数

private DataTable getBasicDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Clear();
            dataTable.Columns.Add("customerID");
            dataTable.Columns.Add("firstName");
            dataTable.Columns.Add("lastName");
            dataTable.Columns.Add("showsNumber");
            dataTable.Columns.Add("visitNumber");
            dataTable.Columns.Add("cancellation");
            return dataTable;
        }

UPDATE2

当我这样做时:

我明白了:

foreach (DataRow row in dataTable.Rows) // Loop over the rows.
                {
                    Console.WriteLine("--- Row ---"); // Print separator.
                    foreach (var item in row.ItemArray) // Loop over the items.
                    {
                        Console.Write("Item: "); // Print label.
                        Console.WriteLine(item); // Invokes ToString abstract method.
                    }
                }


--- Row ---
Item: ImporterOpenTable.Customer
Item:
Item:
Item:
Item:
Item:

UPDATE3

xml

<?xml version="1.0" encoding="ISO-8859-1" ?><root source="ERB" versionmajor="5" versionminor="0" filetype="3PCustomer"><cust rid="303" custid="5" lname="Test" fname="Test" emailoptin="1" mailoptin="1" datecreated="2006-01-12T15:37:54.450" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="408.123.4567" countrycodeid="1" phonetype="Home"/><custcode ccode="Vegetarian"/></cust><cust rid="303" custid="6" lname="John" fname="Doe" email="johnD@ot.com" emailoptin="0" mailoptin="1" cnotes="Half caf double de-caf, with a twist..." datecreated="2006-01-12T17:09:57.013" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="456.456.4565" countrycodeid="1" phonetype="Home"/><custcode ccode="Guest Share"/></cust><cust rid="303" custid="7" webcustid="1654438" lname="doe" fname="john" emailoptin="1" mailoptin="1" datecreated="2006-01-13T11:04:25.653" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="123.456.7890" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="8" lname="Joe" fname="Kennedy" emailoptin="1" mailoptin="1" datecreated="2006-01-16T15:33:08.047" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="777.123.0000" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="9" lname="Abe" fname="Lincoln" emailoptin="1" mailoptin="1" datecreated="2006-01-17T08:52:35.280" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="444.765.4321" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="10" lname="U" fname="Grant" emailoptin="1" mailoptin="1" datecreated="2006-01-17T08:53:29.577" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="415.456.4564" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="11" lname="John-jacob-jingle-hiegmer-schmidt-aka-ve" fname="John-jacob-jingle-hiegmer-schmidt-aka-ve" emailoptin="1" mailoptin="1" datecreated="2006-01-17T10:28:01.217" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="123.456.7890" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="12" lname="Joe" fname="Kennedy" emailoptin="1" mailoptin="1" datecreated="2006-01-17T12:59:23.390" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="777.123.0000" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="13" lname="Joe" fname="Kennedy" emailoptin="1" mailoptin="1" datecreated="2006-01-17T13:21:46.437" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="777.123.0000" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="14" webcustid="1653089" lname="client" fname="WebRes" emailoptin="1" mailoptin="1" datecreated="2006-01-19T11:24:42.403" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="415.555.8989" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="15" webcustid="1654451" lname="DFF 2" fname="Denis" emailoptin="1" mailoptin="1" datecreated="2006-01-19T12:06:24.843" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="415.320.4555" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="16" lname="Andrew" fname="Jackson" emailoptin="1" mailoptin="1" datecreated="2006-01-19T15:36:43.903" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="645.434.6846" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="17" lname="Washington" fname="George" emailoptin="1" mailoptin="1" datecreated="2006-01-19T17:08:26.090" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="18" lname="Washington" fname="George" emailoptin="1" mailoptin="1" datecreated="2006-01-19T17:24:17.560" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="19" lname="Washington" fname="George" emailoptin="0" mailoptin="1" cnotes="5" datecreated="2006-01-19T17:28:55.890" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/><custcode ccode="Loves sweetes"/></cust><cust rid="303" custid="20" lname="L123456789" fname="F0987" emailoptin="1" mailoptin="1" datecreated="2006-01-19T17:30:43.200" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="222.555.8888" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="21" lname="LN1202006" fname="FN83723" emailoptin="1" mailoptin="1" datecreated="2006-01-20T08:37:25.140" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="22" lname="LN1202006" fname="FN8552" emailoptin="1" mailoptin="1" datecreated="2006-01-20T08:55:03.687" count_resos="0" count_noshow="0" count_cancel="1"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="23" lname="LN1202006" fname="FN91333" emailoptin="1" mailoptin="1" datecreated="2006-01-20T09:13:37.543" count_resos="0" count_noshow="0" count_cancel="1"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="24" lname="LN1202006" fname="FN91753" emailoptin="1" mailoptin="1" datecreated="2006-01-20T09:17:54.890" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="25" lname="LN1202006" fname="FN93243" emailoptin="1" mailoptin="1" datecreated="2006-01-20T09:32:44.340" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="26" lname="LN1202006" fname="FN15493" emailoptin="1" mailoptin="1" datecreated="2006-01-20T15:49:05.343" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="999.555.1212" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="27" webcustid="1657237" lname="catlin" fname="bryce" emailoptin="1" mailoptin="1" datecreated="2007-05-14T13:40:19.543" count_resos="0" count_noshow="0" count_cancel="1"><phone phonenumber="415.344.4285" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="28" webcustid="1657088" lname="Mishra" fname="Neena" emailoptin="1" mailoptin="1" datecreated="2007-05-15T11:10:27.450" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="253.464.7586" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="29" webcustid="1657213" lname="nm10" fname="DFF_EastCoast" emailoptin="1" mailoptin="1" datecreated="2007-05-15T11:23:17.153" count_resos="3" count_noshow="1" count_cancel="0"><phone phonenumber="235.456.5766" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="30" webcustid="1656704" lname="Igor" fname="Igor" emailoptin="1" mailoptin="1" datecreated="2007-05-25T16:27:34.297" count_resos="2" count_noshow="0" count_cancel="0"><phone phonenumber="222.222.2222x22222" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="31" lname="Tomorrow" fname="Guest" email="mk6@opentable.com" emailoptin="0" mailoptin="0" cnotes="quite" datecreated="2007-05-29T17:04:22.920" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="415.673.3444" countrycodeid="1" phonetype="Home"/><address address1="123 market st" address2="Room 1600" city="Modesto" state="CA" zip="32457" country="United States"/><custcode ccode="VIP"/></cust><cust rid="303" custid="32" lname="Wait" fname="Wait" emailoptin="1" mailoptin="1" datecreated="2007-05-29T17:09:59.030" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="415.434.3433" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="33" lname="W2" fname="W2" emailoptin="1" mailoptin="1" datecreated="2007-05-29T17:10:27.420" count_resos="0" count_noshow="0" count_cancel="0"><phone phonenumber="949.343.4334" countrycodeid="1" phonetype="Home"/></cust><cust rid="303" custid="36" lname="Lin" fname="Coln" emailoptin="1" mailoptin="1" cnotes="You Bet!" datecreated="2007-05-29T17:29:25.153" count_resos="1" count_noshow="0" count_cancel="0"><phone phonenumber="650.345.4545" countrycodeid="1" phonetype="Home"/><custcode ccode="Investor"/></cust></root>

1 个答案:

答案 0 :(得分:1)

我创建了一个具有自动属性字段的类,如下所示

public class Customers
{
   public int CustomerID {get; set;}
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int ShowsNumber { get; set; }
   public int VisitNumber { get; set; }
   public int Cancellation { get; set; }
}

接下来,我添加了一条路径,指向我从XML文件中读取的位置,更改了在您的计算机上工作的路径 Because I already had a Class in my project named Customer and Field named CustomerID I named it to Customers and CustomerId1 change the code to fit your actual Class Name and Field Names

        List<Customers> customersList =
                        (
                            from e in XDocument.Load(file).Root.Elements("cust")
                            select new Customers
                            {
                                CustomerID = (int)e.Attribute("custid"),
                                FirstName = (string)e.Attribute("fname"),
                                LastName = (string)e.Attribute("lname"),
                                ShowsNumber = (int)e.Attribute("count_noshow"),
                                VisitNumber = (int)e.Attribute("count_resos"),
                                Cancellation = (int)e.Attribute("count_cancel"),
                            }).ToList();
        DataTable dataTable1 = getBasicDataTable();
        ;
        for (int i = 0; i < customersList.Count; i++)
        {
            DataRow datarows = dataTable1.NewRow();
            datarows[0] = customersList[i].CustomerID;
            datarows[1] = customersList[i].FirstName;
            datarows[2] = customersList[i].LastName;
            datarows[3] = customersList[i].ShowsNumber;
            datarows[4] = customersList[i].VisitNumber;
            datarows[5] = customersList[i].Cancellation;
            dataTable1.Rows.Add(datarows);
        }

        for (int i = 0; i < dataTable1.Rows.Count; i++)
        {
            Console.WriteLine(dataTable1.Rows[i]["customerID"]);
        }

接下来我添加了一个静态方法,您需要在最后更改此方法以匹配此

    private static DataTable getBasicDataTable()
    {
        DataTable dataTable = new DataTable();
        dataTable.Clear();
        dataTable.Columns.Add("customerID");
        dataTable.Columns.Add("firstName");
        dataTable.Columns.Add("lastName");
        dataTable.Columns.Add("showsNumber");
        dataTable.Columns.Add("visitNumber");
        dataTable.Columns.Add("cancellation");
        return dataTable;
    }

enter image description here

相关问题