/// <summary> /// Creates the xml containing the calendar month data. /// </summary> /// <param name="dt">The month that will be translated into xml.</param> /// <returns>A memory stream of the created xml.</returns> public MemoryStream GetCalendarXml(DateTime dt) { // Create xml writer. MemoryStream ms = new MemoryStream(); XmlWriter xml = HXml.GetXmlWriterFragment(ms); // Get holidays for the month. Dictionary <DateTime, String> holidays = HDateTime.GetHolidaysInMonth(dt); // Set the first of the month. DateTime startofMonth = HDateTime.FirstOfMonth(dt); // Start <year> xml.WriteStartElement("year"); xml.WriteAttributeString("value", dt.Year.ToString()); // Start <month> xml.WriteStartElement("month"); xml.WriteAttributeString("value", dt.Month.ToString()); // Loop through all the weeks in the month for (int i = 0; i < HDateTime.GetNumberOfWeeks(startofMonth, DayOfWeek.Sunday); i++) { // Set the current day to be the first day in the week, where the first day is a Sunday. DateTime currentDay = HDateTime.FirstOfWeek(startofMonth.AddDays(7 * i), DayOfWeek.Sunday); if (i == 0) { // If this is the beginning of the loop, the first day should always be the first of the current month, which may not be a Sunday. currentDay = startofMonth; } // Start <week> xml.WriteStartElement("week"); // Loop through each day in the week until Saturday, which will indicate that tomorrow is the start of a new week. DayOfWeek nextDay = DayOfWeek.Monday; while (nextDay != DayOfWeek.Sunday && currentDay.Month != startofMonth.AddMonths(1).Month) { // Start <currentDay.DayOfWeek> xml.WriteStartElement(currentDay.DayOfWeek.ToString().ToLower()); xml.WriteAttributeString("day", currentDay.Day.ToString()); if (currentDay.Date.Equals(DateTime.Now.Date)) { // This is today's date. xml.WriteAttributeString("today", "true"); } // Check if this day is a holiday. foreach (KeyValuePair <DateTime, String> holiday in holidays) { if (holiday.Key.Date.Equals(currentDay.Date)) { // Add the holiday to this day. xml.WriteStartElement("event"); xml.WriteValue(holiday.Value); xml.WriteEndElement(); } } // End <currentDay.DayOfWeek> xml.WriteEndElement(); // Increment the day. currentDay = currentDay.AddDays(1); nextDay = currentDay.DayOfWeek; } // End <week> xml.WriteEndElement(); } // End <month> xml.WriteEndElement(); // End <year> xml.WriteEndElement(); // Close the writer. xml.Flush(); xml.Close(); // Set memory stream position back to the beginning. ms.Position = 0; return(ms); }