我需要一些帮助。我有一个XML账单,有多个相同的节点。现在我循环遍历节点并相应地合并值。我向你展示的代码只是我需要帮助的部分。循环包含更多我将值合并在一起的地方。我需要帮助的是选择MIN和MAX日期/用法。由于我现在返回多个节点而不是一个节点,我将循环并合并数据。
这就是我所坚持的。我已经完成了合并我需要的所有其他数据。我18岁,是一个非常新的程序员。我只是迷失在自己的逻辑中。任何指导都会帮助我。我相信最后两个IF语句是我需要帮助的地方,也是逻辑的所在。
答案 0 :(得分:0)
我认为你想要做的就是首先遍历所有项目。将它们转换为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;
}
当然,我没有测试过这些东西,但它应该让你去,你可以解决这些问题。既然你是编程新手,你明白我做了什么吗?