static HolidayUtility() { string path=Path.Combine( AppDomain.CurrentDomain.BaseDirectory,"Config/HolidayData.xml"); XDocument doc = XDocument.Load(path); var items = doc.Descendants("Item"); foreach (var item in items) { var isHoliday = item.Elements("IsHoliday").Select(o => o.Value).FirstOrDefault() == "true"; var strDate = item.Elements("Date").Select(o => o.Value).FirstOrDefault(); DateTime dt; if (DateTime.TryParse(strDate, out dt)) { var holidayItem = new HolidayItem(); holidayItem.Name = item.Attributes("Name").Select(o => o.Value).FirstOrDefault(); holidayItem.Date = dt; holidayItem.IsHoliday = isHoliday; holidayItem.HolidayType = isHoliday ? 2 : 1; holidayItems.Add(holidayItem); } } InitNoRestHoliday(); }
/// <summary> /// 添加不休息的假日,例如(情人节) /// </summary> public static List<HolidayItem> AppendNoRestHoliday(List<HolidayItem> holidayItems) { if (noRestHolidayItems.Count > 0) { List<HolidayItem> result = new List<HolidayItem>(); result.AddRange(holidayItems); foreach (var noRestHolidayItem in noRestHolidayItems) { var holidayItem = holidayItems.FirstOrDefault(o => o.Date == noRestHolidayItem.Date); if (holidayItem == null) { result.Add(noRestHolidayItem); } else//如果和法定节假日重叠 { HolidayItem data = new HolidayItem() { Date = holidayItem.Date, IsHoliday = holidayItem.IsHoliday, HolidayType = holidayItem.HolidayType, }; //如果是法定节日,有些显示法定节日名称 data.Name = holidayItem.IsHoliday && !string.IsNullOrWhiteSpace(holidayItem.Name) ? holidayItem.Name : noRestHolidayItem.Name; result.Add(data); } } return result; }else { return holidayItems; } }
private static void InitNoRestHoliday() { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/NoRestHolidayData.xml"); if (File.Exists(path)) { XDocument doc = XDocument.Load(path); var items = doc.Descendants("Item"); foreach (var item in items) { var strDate = item.Elements("Date").Select(o => o.Value).FirstOrDefault(); DateTime dt; if (DateTime.TryParse(strDate, out dt)) { var noRestHolidayItem = new HolidayItem(); noRestHolidayItem.Name = item.Attributes("Name").Select(o => o.Value).FirstOrDefault(); noRestHolidayItem.Date = dt; noRestHolidayItem.IsHoliday = false; noRestHolidayItem.HolidayType = 3; noRestHolidayItems.Add(noRestHolidayItem); } } } }