//Insert post into today's XML file public void InsertPost(BlogPost newPost) { TypeSerialization ts = new TypeSerialization(); XMLIOHandler xio = new XMLIOHandler(); if (String.IsNullOrEmpty(newPost.ID)) { newPost.ID = GeneratePostID(); } System.Collections.Generic.List <BlogPost> newPostCol = new System.Collections.Generic.List <BlogPost>(); newPostCol.Add(newPost); if (System.IO.File.Exists(dataPath + "\\" + GetOwningXMLFileName(newPost.ID))) { foreach (BlogPost oldPost in ts.DeSerializePosts(xio.GetXMLString(GetOwningXMLFileName(newPost.ID)))) { newPostCol.Add(oldPost); } } if (System.IO.File.Exists(dataPath + GetOwningXMLFileName(newPost.ID))) { xio.RmDataFile(GetOwningXMLFileName(newPost.ID)); } if (newPostCol.Count > 0) { xio.WriteXML(ts.SerializeGenericObject(newPostCol), GetOwningXMLFileName(newPost.ID)); } }
public System.Collections.Generic.List <BlogPost> GetPostsForPage(int pageNumber) { XMLIOHandler xio = new XMLIOHandler(); TypeSerialization ts = new TypeSerialization(); System.Collections.Generic.List <BlogPost> postCol = new System.Collections.Generic.List <BlogPost>(); foreach (string fileName in GetAllBlogFiles()) { foreach (BlogPost tempPost in ts.DeSerializePosts(xio.GetXMLString(fileName))) { if (postCol.Count <= 10) { postCol.Add(tempPost); } else { break; } } if (postCol.Count >= 10) { break; } } return(postCol); }
//Deletes post with the ID passed. This function reads in existing XML from owning XML file, //rebuilds the collection without the deleted post, deletes the owning XML file, and rewrites //the owning XML file with the same name. public void DeletePostByID(string postID) { XMLIOHandler xio = new XMLIOHandler(); TypeSerialization ts = new TypeSerialization(); System.Collections.Generic.List <BlogPost> newPostCol = new System.Collections.Generic.List <BlogPost>(); foreach (BlogPost tempPost in ts.DeSerializePosts(xio.GetXMLString(GetOwningXMLFileName(postID)))) { if (tempPost.ID != postID) { newPostCol.Add(tempPost); } } xio.RmDataFile(GetOwningXMLFileName(postID)); if (newPostCol.Count > 0) { xio.WriteXML(ts.SerializeGenericObject(newPostCol), GetOwningXMLFileName(postID)); } }
public System.Collections.Generic.List <BlogPost> GetPostsForDay(System.DateTime requestedDate) { XMLIOHandler xio = new XMLIOHandler(); TypeSerialization ts = new TypeSerialization(); System.Collections.Generic.List <BlogPost> retPosts = new System.Collections.Generic.List <BlogPost>(); if (System.IO.File.Exists(dataPath + "\\" + GetXMLFileForDay(requestedDate))) { retPosts = ts.DeSerializePosts(xio.GetXMLString(GetXMLFileForDay(requestedDate))); } if (retPosts.Count > 0) { return(retPosts); } else { return(ts.DeSerializePosts(xio.GetXMLString("20070101.xml"))); } }
//Gets post from passed ID public BlogPost GetPostById(string postID) { XMLIOHandler xio = new XMLIOHandler(); TypeSerialization ts = new TypeSerialization(); BlogPost sorryPost = new BlogPost(); foreach (BlogPost tempPost in ts.DeSerializePosts(xio.GetXMLString(GetOwningXMLFileName(postID)))) { if (tempPost.ID == postID) { return(tempPost); } else { sorryPost.Title = "Post Not Found"; sorryPost.Text = "The post you requested was not found"; } } return(sorryPost); }