【问题标题】:finding the min date/usage and max date/usage查找最小日期/使用情况和最大日期/使用情况
【发布时间】:2015-07-24 17:45:13
【问题描述】:

我需要一些帮助。我有一个包含多个相同节点的 XML 账单。现在我循环遍历节点并相应地合并值。我向您展示的代码只是我需要帮助的部分。循环包含更多我将值合并在一起的地方。我需要帮助的是选择 MIN 和 MAX 日期/用法。由于我现在返回多个节点而不是一个节点,因此我将循环并合并数据。

  1. 因此,在它遍历节点后,我需要将 UsageMeterReadStartDate 设置为最早的 MIN 日期。数据类型字符串
  2. 与 UsageMeterReadStartUsage 相同。数据类型字符串
  3. 因此,在它遍历节点后,我需要将 UsageMeterReadStartDate 设置为最新的 MAX 日期。数据类型字符串
  4. 与 UsageMeterReadEndUsage 相同

这就是我所坚持的。我已经完成了我需要的所有其他数据的合并。我今年 18 岁,是一个相当新的程序员。我只是迷失在自己的逻辑中。任何指导都会帮助我。我相信最后两个 IF 语句是我需要帮助的地方,也是逻辑所在。

【问题讨论】:

  • 您只是假设年份是当前年份吗?我没有看到你在任何地方解析它。您可以将月/日放入一个 DateTime 对象中,该对象允许您比较以前的对象。我看到的一个问题是您一遍又一遍地使用saBillDetail 变量。所以每次循环时,都会覆盖之前捕获的信息
  • @Thraka 我正在其他地方进行解析。我只需要例如将 saBillDetail.UsageMeterReadEndDate 设置为其循环通过的节点的最早日期。您可以在我发布的stackoverflow.com/questions/31164287/…stackoverflow.com/questions/31164287/… 的另一个问题中看到更多我的代码,感谢您的帮助
  • 你的 xml 是什么样的? (特别是那些节点)
  • @Thraka 我为 UsageMeterReadStartDate 和 UsageMeterReadStartUsage 编辑并添加了部分 xml。 readStart 将返回 1 月 14 日,使用将返回 20891
  • 如果 14 是一年,而不是我想象的一天,请再次检查我的答案

标签: c# xml c#-4.0


【解决方案1】:

我认为您想要做的是首先遍历所有项目。将这些转换为 DateTime 对象。 DateTime 对象允许您比较其他对象的大于或小于。不过,我认为,您会陷入年份转换,因为您没有在源代码中跟踪它。

if (meterReadStartXMLNodes.Count > 0 && meterReadStartXMLNodes[0].HasChildNodes)
{   // This is to fill up the meter read start date and meter read start usage as an attribute of the "sadetails" node in the newer XML. 
    DateTime oldDateTime = DateTime.Now;
    string oldUsage = "";
    foreach (var node in meterReadStartXMLNodes)
    {
        DateTime tempDateTime = DateTime.Parse(String.Format("{0} {1}", meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_STRT_DT_MM.USAGE").InnerText,
                                                                        meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_STRT_DT_DD.USAGE").InnerText));

        if (tempDateTime < oldDateTime)
        {
            // Any time you've determined you have an older date, we capture the usage for that date
            oldDateTime = tempDateTime;
            oldUsage = meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_KWH_PRV_MTR_READ.USAGE").InnerText;
        }
    }

    // By the time you get here, you've already determined the lowest date/time, format it.
    saBillDetail.UsageMeterReadStartDate = oldDateTime.ToString("MMM dd");
    saBillDetail.UsageMeterReadStartUsage = oldUsage;
}

对于结束日期,您可以做同样的事情,除了不需要为其分配值,它将默认为 0001 年。

if (meterReadEndXMLNodes.Count > 0 && meterReadEndXMLNodes[0].HasChildNodes)
{   // This is to fill up the meter read end date and meter read end usage as an attribute of the "sadetails" node in the newer XML.

    DateTime latestDateTime = new DateTime();
    string latestUsage = "";
    foreach (var node in meterReadStartXMLNodes)
    {
        DateTime tempDateTime = DateTime.Parse(String.Format("{0} {1}", meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_END_DT_MM.USAGE").InnerText,
                                                                        meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_END_DT_DD.USAGE").InnerText));

        if (tempDateTime > latestDateTime)
        {
            // Any time you've determined you have an newer date, we capture the usage for that date
            latestDateTime = tempDateTime;
            latestUsage = meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_KWH_MTR_READ.USAGE").InnerText;
        }
    }

    saBillDetail.UsageMeterReadEndDate = latestDateTime.ToString("MMM dd");
    saBillDetail.UsageMeterReadEndUsage = latestUsage;
}

当然,我没有测试这些东西,但它应该能让你继续前进,你可以修复错误。既然你是编程新手,你明白我的所作所为吗?

【讨论】:

  • 你的 1 月 14 日不是这一天吗? (第十四部分)
  • 只是月份和日期。不是年份
  • 好的,我回滚了处理日期而不是年份的答案:)
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 2018-06-06
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
相关资源
最近更新 更多