在C#

时间:2017-12-07 21:49:44

标签: c# xml algorithm

我正在尝试从XML文档中提取数据,并根据C#中的时间汇总数据。

例如 - 我有以下XML文档:

文件1:

                "<HourlyTotals>" +
                "<Data>" +
                "<Time>08:00</Time>" +
                "<NetSales>200.25</NetSales>" +
                "<NetReturns>15.25</NetReturns>" +
                "<NetIncome>185.00</NetIncome>" +
                "<CustomerCount>15</CustomerCount>" +
                "<PercentOfIncome>3</PercentOfIncome>" +
                "</Data>" +
                "<Data>" +
                "<Time>08:15</Time>" +
                "<NetSales>100.25</NetSales>" +
                "<NetReturns>15.25</NetReturns>" +
                "<NetIncome>85.00</NetIncome>" +
                "<CustomerCount>2</CustomerCount>" +
                "<PercentOfIncome>1.5</PercentOfIncome>" +
                "</Data>" +
                 "<Data>" +
                "<Time>08:30</Time>" +
                "<NetSales>100.25</NetSales>" +
                "<NetReturns>15.25</NetReturns>" +
                "<NetIncome>85.00</NetIncome>" +
                "<CustomerCount>2</CustomerCount>" +
                "<PercentOfIncome>1.5</PercentOfIncome>" +
                "</Data>" +
                "<Data>" +
                "<Time>10:00</Time>" +
                "<NetSales>100.25</NetSales>" +
                "<NetReturns>10.25</NetReturns>" +
                "<NetIncome>176.00</NetIncome>" +
                "<CustomerCount>10</CustomerCount>" +
                "<PercentOfIncome>1</PercentOfIncome>" +
                "</Data>" +
                "</HourlyTotals>"

请忽略引号和'+':这是因为我试图创建虚假XML数据并将其作为字符串传递给C#中的列表。

文件2:

            "<HourlyTotals>" +
            "<Data>" +
            "<Time>08:00</Time>" +
            "<NetSales>100.25</NetSales>" +
            "<NetReturns>10.25</NetReturns>" +
            "<NetIncome>176.00</NetIncome>" +
            "<CustomerCount>10</CustomerCount>" +
            "<PercentOfIncome>1</PercentOfIncome>" +
            "</Data>" +
            "<Data>" +
            "<Time>08:15</Time>" +
            "<NetSales>120.00</NetSales>" +
            "<NetReturns>11.00</NetReturns>" +
            "<NetIncome>54.00</NetIncome>" +
            "<CustomerCount>5</CustomerCount>" +
            "<PercentOfIncome>2.3</PercentOfIncome>" +
            "</Data>" +
             "<Data>" +
            "<Time>08:30</Time>" +
            "<NetSales>160.11</NetSales>" +
            "<NetReturns>17.25</NetReturns>" +
            "<NetIncome>34.00</NetIncome>" +
            "<CustomerCount>4</CustomerCount>" +
            "<PercentOfIncome>3.2</PercentOfIncome>" +
            "</Data>" +
            "<Data>" +
            "<Time>09:15</Time>" +
            "<NetSales>100.25</NetSales>" +
            "<NetReturns>10.25</NetReturns>" +
            "<NetIncome>176.00</NetIncome>" +
            "<CustomerCount>10</CustomerCount>" +
            "<PercentOfIncome>1</PercentOfIncome>" +
            "</Data>" +
            "<Data>" +
            "<Time>10:00</Time>" +
            "<NetSales>100.25</NetSales>" +
            "<NetReturns>10.25</NetReturns>" +
            "<NetIncome>176.00</NetIncome>" +
            "<CustomerCount>10</CustomerCount>" +
            "<PercentOfIncome>1</PercentOfIncome>" +
            "</Data>" +
            "</HourlyTotals>"

文件3:

            "<HourlyTotals>" +
            "<Data>" +
            "<Time>08:00</Time>" +
            "<NetSales>22.25</NetSales>" +
            "<NetReturns>21.25</NetReturns>" +
            "<NetIncome>122.00</NetIncome>" +
            "<CustomerCount>3</CustomerCount>" +
            "<PercentOfIncome>6</PercentOfIncome>" +
            "</Data>" +
            "<Data>" +
            "<Time>08:15</Time>" +
            "<NetSales>174.00</NetSales>" +
            "<NetReturns>16.00</NetReturns>" +
            "<NetIncome>68.00</NetIncome>" +
            "<CustomerCount>2</CustomerCount>" +
            "<PercentOfIncome>0.2</PercentOfIncome>" +
            "</Data>" +
             "<Data>" +
            "<Time>08:30</Time>" +
            "<NetSales>99.00</NetSales>" +
            "<NetReturns>88.00</NetReturns>" +
            "<NetIncome>69.00</NetIncome>" +
            "<CustomerCount>6</CustomerCount>" +
            "<PercentOfIncome>9</PercentOfIncome>" +
            "</Data>" +
            "<Data>" +
            "<Time>09:30</Time>" +
            "<NetSales>100.25</NetSales>" +
            "<NetReturns>10.25</NetReturns>" +
            "<NetIncome>176.00</NetIncome>" +
            "<CustomerCount>10</CustomerCount>" +
            "<PercentOfIncome>1</PercentOfIncome>" +
            "</Data>" +
            "</HourlyTotals>"

我希望按时间汇总NetSales,NetReturns,NetIncome,CustomerCount和PercentOfIncome值。例如,时间8:00应该具有值   322.75 (200.25 + 100.25 + 22.25)作为NetSales值等等。

时间节点值的范围为8:00到23:45。我试图找出一种有效的方法来提取所有时间间隔并根据它们汇总值。

到目前为止,我已编写代码从XML中提取单个节点的数据:

foreach (var selector in fakeXML)
        {
            XDocument doc = XDocument.Parse(selector);
            var data = doc.Descendants("Data");
            var result1 = from q in data
                          select new
                          {
                              Time = q.Element("Time").Value,
                              NetSales = q.Element("NetSales").Value,
                              NetReturns = q.Element("NetReturns").Value,
                              NetIncome = q.Element("NetIncome").Value,
                              CustCount = q.Element("CustomerCount").Value,
                              PercentOfIncome = q.Element("PercentOfIncome").Value
                          }

        }  

fakeXML是一个字符串列表,其中包含单个XML文档作为字符串。

这段代码只允许我从XML文档中提取单个节点。

我无法根据时间节点的值找出一种有效的方法。任何关于如何进行的线索都会非常棒!

提前致谢。

1 个答案:

答案 0 :(得分:1)

获得所有数据后。按Time分组,然后Sum将每组中的值分组。

var data = fakeXML.SelectMany(selector => {
    XDocument doc = XDocument.Parse(selector);
    return from q in doc.Descendants("Data")
           select new {
               Time = q.Element("Time").Value,
               NetSales = (decimal)q.Element("NetSales"),
               NetReturns = (decimal)q.Element("NetReturns"),
               NetIncome = (decimal)q.Element("NetIncome"),
               CustCount = (int)q.Element("CustomerCount"),
               PercentOfIncome = (double)q.Element("PercentOfIncome")
           };
});

var summary = data.GroupBy(d => d.Time)
    .Select(g => new {
        Time = g.Key,
        NetSales = g.Sum(d => d.NetSales),
        NetReturns = g.Sum(d => d.NetReturns),
        NetIncome = g.Sum(d => d.NetIncome),
        CustCount = g.Sum(d => d.CustCount),
    });

摘要集合中的每个项目都有按时间计算的总计。