public static bool SaveConfig() { string path = AppDataPath + "rssreader.config.xml"; if (File.Exists(path)) { File.Delete(AppDataPath + "rssreader.config.xml"); } Logger.Instance.Log("mARC Rss RT Reader Application", "saving info to config file"); XDocument doc = new XDocument( new XElement("config", Config.Select( x => new XElement("feed", new XAttribute("id", x.ID), new XAttribute("title", x.Title), new XAttribute("url", x.URL), new XAttribute("file", x.File) ) )) ); try { XElement xE = doc.Root; TrackingDocument[] docs = new TrackingDocument[mainform.trackingDocscheckedListBox.Items.Count]; TrackingDocument d; string similardocs = ""; for (int i = 0; i < mainform.trackingDocscheckedListBox.Items.Count; i++) { d = (TrackingDocument)mainform.trackingDocscheckedListBox.Items[i]; similardocs = ":"; foreach (string s in d.similarDocs) { similardocs += s + ":"; } xE.Add(new XElement("trackingDocument", new XAttribute("dbID", d.dbID), new XAttribute("link", d.link), new XAttribute("title", d.title), new XAttribute("pubDate", d.pubDate), new XAttribute("similarDocs", similardocs))); } doc.Save(path); Logger.Instance.Log("mARC Rss RT Reader Application", "saved"); return(true); } catch (Exception eee) { MessageBox.Show(eee.StackTrace + ": " + eee.Message); return(false); } }
public static int GetConfig() { if (Directory.Exists(AppDataPath)) { try { XDocument doc = XDocument.Load(AppDataPath + "rssreader.config.xml"); try { foreach (XElement feed in doc.Root.Elements("feed")) { Config.Add( new RSSRTReader.Misc.Config( System.Int32.Parse(feed.Attribute("id").Value), feed.Attribute("title").Value, feed.Attribute("url").Value, feed.Attribute("file").Value ) ); mainform.FeedsTable.Rows.Add(new string[] { feed.Attribute("title").Value, feed.Attribute("url").Value }); mainform.FeedscheckedListBox.Items.Add(feed.Attribute("title").Value); } TrackingDocument trdoc; foreach (XElement d in doc.Root.Elements("trackingDocument")) { trdoc = new TrackingDocument(); trdoc.dbID = d.Attribute("dbID").Value; trdoc.link = d.Attribute("link").Value; trdoc.title = d.Attribute("title").Value; trdoc.pubDate = d.Attribute("pubDate").Value; XAttribute a = d.Attribute("similarDocs"); if (a != null) { string sdocs = a.Value; string[] docs = sdocs.Split(':'); trdoc.similarDocs.AddRange(docs); // on cherche tous les éléments vides //IEnumerable<string> nullItems = from string s in trdoc.similarDocs where string.IsNullOrEmpty(s) == null select s; // et on les bute trdoc.similarDocs.RemoveAll(item => string.IsNullOrEmpty(item) == true); } mainform.trackingDocscheckedListBox.Items.Add(trdoc); } } catch (NullReferenceException) { Logger.Instance.Log("mARC Rss RT Reader Application", "config file is empty"); Logger.Instance.Log("mARC Rss RT Reader Application", "consider adding some feeds"); return(-1); } return(0); } catch (FileNotFoundException) { Logger.Instance.Log("mARC Rss RT Reader Application", "no config file found. creating..."); try { XDocument doc = new XDocument(new XElement("config")); doc.Save(AppDataPath + "rssreader.config.xml"); } catch (Exception ex) { Logger.Instance.Log("mARC Rss RT Reader Application", "could not create config file."); return(-2); throw (new CreateConfigFileException("could not create config file", ex)); } Logger.Instance.Log("mARC Rss RT Reader Application", "created"); return(-3); } catch (UriFormatException) { Logger.Instance.Log("mARC Rss RT Reader Application", "invalid xml file. please delete it."); return(-4); } } else { Logger.Instance.Log("mARC RT Rss Reader Application", "directory not found. creating..."); try { Directory.CreateDirectory(AppDataPath); } catch (Exception ex) { Logger.Instance.Log("mARC Rss RT Reader Application", "could not create config directory."); return(-5); throw (new CreateConfigDirException("could not create config directory", ex)); } Logger.Instance.Log("mARC Rss RT Reader Application", "created"); Logger.Instance.Log("mARC Rss RT Reader Application", "creating config file..."); try { XDocument doc = new XDocument(new XElement("config")); doc.Save(AppDataPath + "rssreader.config.xml"); } catch (Exception ex) { Logger.Instance.Log("mARC Rss RT Reader Application", "could not create config file."); return(-6); throw (new CreateConfigFileException("could not create config file", ex)); } Logger.Instance.Log("mARC Rss RT Reader Application", "created"); return(0); } }