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

马可·迪纳佐利(Marco Dinatsoli)

这是我的代码

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在datatable行中打印column的值但是控制台会打印以下内容:

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

更新1

为了给您全貌,这是我的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;
        }

更新2

当我这样做时:

我懂了:

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:

更新3

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="[email protected]" 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="[email protected]" 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>
方法人

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

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

如何添加数据表自定义行渲染/聚合

来自分类Dev

数据表自定义字段

来自分类Dev

数据表按钮 - 自定义

来自分类Dev

数据透视表中的自定义行标签

来自分类Dev

具有自定义行的ListView引发InflaterException

来自分类Dev

具有自定义行为的 CollapsingToolbarLayout

来自分类Dev

使用awk打印自定义行

来自分类Dev

具有自定义选项的最佳Angular数据表最适合创建企业应用程序?

来自分类Dev

从自定义行组成物料表

来自分类Dev

具有自定义谓词的std :: unordered_set中的未定义行为

来自分类Dev

数据表:自定义响应处理

来自分类Dev

Primefaces数据表自定义分页未更新

来自分类Dev

如何自定义角度数据表的样式

来自分类Dev

使用数据表的按钮进行自定义过滤

来自分类Dev

自定义粘贴到空数据表中

来自分类Dev

自定义过滤器到数据表

来自分类Dev

jquery数据表中的自定义搜索

来自分类Dev

使用 JQuery 数据表进行自定义排序

来自分类Dev

jquery 数据表自定义标题悬停样式

来自分类Dev

从数据表中的“自定义选择”字段发送自定义数据

来自分类Dev

具有自定义数据的ListView

来自分类Dev

具有自定义行布局的多选警报对话框

来自分类Dev

具有数据表聚合的用户定义函数

来自分类Dev

需要在数据表标题中显示自定义信息消息,是否有内置选项?

来自分类Dev

动态创建具有自定义名称的表并插入具有自定义表名称的表

来自分类Dev

具有自定义边框的CSS表

来自分类Dev

具有API令牌的Laravel自定义表

来自分类Dev

数据表AJAX数据源和自定义分页操作