public static List <Banner> DeserializeXMLToBannersLight(string filefolder, string fileName, int amount) { try { string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + filefolder + "\\" + fileName; List <Banner> bannersLight = new List <Banner>(); DateTime fileCreationDate = System.IO.File.GetCreationTime(fileLocation); bool isOld = fileCreationDate < DateTime.Now.AddDays(-1) ? true : false; if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <Banner>)); System.IO.FileStream file = System.IO.File.OpenRead(fileLocation); bannersLight = (List <Banner>)writer.Deserialize(file); bannersLight = bannersLight.Take(amount).ToList(); file.Close(); } else { bannersLight = QueryLatestBannersForXml(amount); System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <Banner>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, bannersLight); file.Close(); } return(bannersLight.Take(amount).ToList()); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public static WeatherStatus DeserializeXMLToWeathers(string filefolder, string fileName, string cityName) { try { string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + filefolder + "\\" + fileName; List <WeatherStatus> weatherStatuses = new List <WeatherStatus>(); WeatherStatus cityWeatherStatus = new WeatherStatus(); if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <WeatherStatus>)); System.IO.FileStream file = System.IO.File.OpenRead(fileLocation); weatherStatuses = (List <WeatherStatus>)writer.Deserialize(file); cityWeatherStatus = weatherStatuses.Single(x => x.City == cityName); file.Close(); } else { weatherStatuses = QueryAllWeatherStatusesForXml(); System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <WeatherStatus>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, weatherStatuses); file.Close(); } return(weatherStatuses.Single(x => x.City == cityName)); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public static List <PostCategory> DeserializeXMLToPostCategories(string filefolder, string fileName) { try { string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + filefolder + "\\" + fileName; List <PostCategory> categories = new List <PostCategory>(); if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <PostCategory>)); System.IO.FileStream file = System.IO.File.OpenRead(fileLocation); categories = (List <PostCategory>)writer.Deserialize(file); file.Close(); } else { categories = QueryPostCategories(PostModel.postsMainSiteUrl, PostModel.categoryListName, PostModel.getCategoriesCamlQuery); System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <PostCategory>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, categories); file.Close(); } return(categories); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public static List <Post> DeserializeXMLToPostsLight(string filefolder, string fileName, int amount) { try { string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + filefolder + "\\" + fileName; List <Post> postsLight = new List <Post>(); if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <Post>)); System.IO.FileStream file = System.IO.File.OpenRead(fileLocation); postsLight = (List <Post>)writer.Deserialize(file); file.Close(); } else { postsLight = QueryAllLatestPostsLight(); System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <Post>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, postsLight); file.Close(); } return(postsLight.Take(amount).ToList()); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(null); } }
public bool CreateOrUpdateCategoriesXML(string fileLocation) { try { List <PostCategory> categories = QueryPostCategories(); if (HLP.CheckIfFileExists(fileLocation)) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <PostCategory>)); System.IO.FileStream file = System.IO.File.OpenWrite(fileLocation); writer.Serialize(file, categories); file.Close(); } else { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List <PostCategory>)); System.IO.FileStream file = System.IO.File.Create(fileLocation); writer.Serialize(file, categories); file.Close(); } return(true); } catch (Exception ex) { EXP.RedirectToErrorPage(ex.Message); return(false); } }