I am trying to make the situation like this: user can either fill in “just txtComStartDate” or “just txtComEndDate” or “both txtComStartDate & txtComEndDate”.
If user fill in txtComStartDate as 18/08/12, only the data from 18/08/12 onwards will display.
If user fill in txtComEndDate as 19/08/12, only the data before 19/08/12 onwards will display.
If user limit both txtComStartDate 18/08/12 and txtComEndDate 19/08/12, the data between these two dates will display.
The if statement is giving me error and not working properly. I am confused about the && and ||. Could someone help? You help would be much appreciated. Thank you.
My code:
private void searchComByDate()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement)node;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
CultureInfo provider = CultureInfo.InvariantCulture;
if (txtComStartDate.Text != (String.Empty) && DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider)
|| txtComEndDate.Text != (String.Empty) && DateTime.Parse(itemDate) <= DateTime.ParseExact(txtComEndDate.Text, "dd/MM/yy", provider)
|| (txtComStartDate.Text != (String.Empty) && DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider) && txtComEndDate.Text != (String.Empty) && DateTime.Parse(itemDate) <= DateTime.ParseExact(txtComEndDate.Text, "dd/MM/yy", provider)))
{
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);
richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
"\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
}
}
}
}
Your short circuiting is failing, because it gets to the
DateTime.Parse(txtComStartDate.Text)and fails. You should allow it to short circuit when any of those fail to ignore the results:The way you’re going to read this is “Either I can’t parse the start date (meaning the user omitted it or it’s not valid) or the item date is greater than the start date; AND either the end date can’t be parsed (again, either omitted or invalid) or the item date is less than the end date.”
Take advantage of the short circuit here. If you can’t parse the start date or end date, then it won’t bother checking the item date for that boundary. However, because of the
&&, it will check both (unless the item date is before the specified start date, in which case it can skip the record).