// Display posts private static void displayPosts(PostList postList) { if (postList == null) { Console.WriteLine("Blog posts were not set up!"); return; } if (postList.Posts.Count() == 0) { Console.WriteLine("No blog posts available to display!"); return; } Console.WriteLine("# Blog Posts: {0}", postList.Posts.Count()); Console.WriteLine("---------------------------------------------------------------------------"); Console.WriteLine(" "); int i = 1; foreach (Post post in postList.Posts) { Console.WriteLine("Post # {0}", i); Console.WriteLine("Blog Title: {0}", post.BlogInformation.Title); Console.WriteLine("Blog Link: {0}", post.BlogInformation.Link); Console.WriteLine("Blog Description: {0}", post.BlogInformation.Description); Console.WriteLine("Post Title: {0}", post.Title); Console.WriteLine("Post Publication Date: {0}", post.PublicationDate); Console.WriteLine("Post Link: {0}", post.Link); Console.WriteLine("Post Description:"); Console.WriteLine(post.Description); Console.WriteLine("Post Content:"); Console.WriteLine(post.Content); Console.WriteLine("---------------------------------------------------------------------------"); Console.WriteLine(" "); ++i; } }
// Get the blog posts from the XML data via XMLDocument, // and return a PostList object private static PostList getBlogPostsXMLDoc(XmlDocument xmlData, BlogInfo blogInfo) { var postList = new PostList(); // Get the list of blog post XML nodes XmlNodeList blogPostXMLNodes = xmlData.SelectNodes(Constants.BlogPostXMLNode); // Set up a post object from each blog post data foreach (XmlNode blogPostXML in blogPostXMLNodes) { postList.Posts.Add(new Post { BlogInformation = blogInfo, Content = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLContentIndex).InnerText, Description = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLDescriptionIndex).InnerText, Link = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLLinkIndex).InnerText, PublicationDate = Convert.ToDateTime(blogPostXML.ChildNodes.Item(Constants.BlogPostXMLPublicationDateIndex).InnerText), Title = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLTitleIndex).InnerText }); /* string blogPostPubDateXMLNodeName = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLPublicationDateIndex).Name; string blogPostTitleXMLNodeName = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLTitleIndex).Name; string blogPostLinkXMLNodeName = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLLinkIndex).Name; string blogPostDescrXMLNodeName = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLDescriptionIndex).Name; string blogPostContentXMLNodeName = blogPostXML.ChildNodes.Item(Constants.BlogPostXMLContentIndex).Name; */ } return postList; }
// Get the blog posts from the XML data via Linq to XML, // and return a PostList object private static PostList getBlogPostsLinqXML(XDocument xmlDoc, BlogInfo blogInfo) { var postList = new PostList(); //XNamespace contentNameSpace = "http://purl.org/rss/1.0/modules/content/"; XNamespace contentNameSpace = getContentNameSpace(xmlDoc); var docNameSpace = xmlDoc.Root.Name.Namespace; // Get the blog posts from the XML document // They are in <item> tags, which are under <channel> under <rss> postList.Posts = xmlDoc.Element("rss").Element("channel").Descendants("item").Select(post => new Post //xmlDoc.Descendants("item").Select(post => new Post { BlogInformation = blogInfo, Content = post.Element(contentNameSpace + "encoded").Value, Description = post.Element("description").Value, Link = post.Element("link").Value, PublicationDate = Convert.ToDateTime(post.Element("pubDate").Value), Title = post.Element("title").Value }).ToList(); /* var blogPosts = xmlDoc.Element("rss").Element("channel").Descendants("item"); foreach (var post in blogPosts) { var newPost = new Post(); newPost.BlogInformation = blogInfo; XElement contentElement = post.Element(contentNameSpace + "encoded"); newPost.Content = post.Element(contentNameSpace + "encoded").Value; newPost.Link = post.Element("link").Value; newPost.PublicationDate = Convert.ToDateTime(post.Element("pubDate").Value); newPost.Title = post.Element("title").Value; newPost.Description = post.Element("description").Value; postList.Posts.Add(newPost); } */ return postList; }