//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)); } }
//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)); } }