/// <summary> /// Gets a list of archive months - id / year / month / count /// Used by archive user control to display the tree-like structure /// </summary> /// <returns>List of ArchiveMonth objects</returns> public List<ArchiveMonth> GetArchiveMonths() { List<ArchiveMonth> months = new List<ArchiveMonth>(); XElement root = null; try { root = XElement.Load(this._path); } catch (Exception ex) { // missing file // invalid XML in file Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } // No Archive month foreach (XElement monthElem in root.Elements("ArchiveMonth")) { // missing attribute ArchiveMonth month = new ArchiveMonth(); try { // missing attribute month.ID = int.Parse(monthElem.Attribute("ID").Value); month.Month = int.Parse(monthElem.Attribute("Month").Value); month.Year = int.Parse(monthElem.Attribute("Year").Value); month.Count = int.Parse(monthElem.Attribute("Count").Value); } catch (Exception ex) { Logger.Log(FORMAT_ERROR, ex); continue; } months.Add(month); } return months; }
/// <summary> /// Create a new node in Archive file for a new month /// Contains ID, month, year, count /// Called when a new post is published on a new month /// </summary> /// <param name="month"> /// The Archive Month /// </param> public void Create(ArchiveMonth month) { XElement root = null; try { // file not found // invalid XML root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } // check if month exists, Do not create, if exists var qry = from elem in root.Elements("ArchiveMonth") where (int)elem.Attribute("ID") == month.ID select elem; if (qry.Count<XElement>() > 0) { Logger.Log(DUP_MONTH_ERROR); throw new ApplicationException(DUP_MONTH_ERROR); } XElement monthElem = new XElement( "ArchiveMonth", new XAttribute("ID", month.ID), new XAttribute("Year", month.Year), new XAttribute("Month", month.Month), new XAttribute("Count", month.Count)); // first element (latest post in new month) // insert somewhere in the past where no post for a month bool lastNode = true; foreach (XElement refElem in root.Elements("ArchiveMonth")) { int refArchID = (int)refElem.Attribute("ID"); if (month.ID > refArchID) { refElem.AddBeforeSelf(monthElem); lastNode = false; break; } } // only element // last element in the list if (lastNode) { root.Add(monthElem); } root.Save(this._path); }