// Get the blog information from the XML data via Linq to XML, // and set it in the returned blog info object private static BlogInfo getBlogInfoLinqXML(XDocument xmlDoc) { var blogInfo = new BlogInfo(); // Get the blog information XML node var blogInfoXML = xmlDoc.Element("rss").Element("channel"); // Set the blog information in the blog info object blogInfo.Description = blogInfoXML.Element("description").Value; blogInfo.Link = blogInfoXML.Element("link").Value; blogInfo.Title = blogInfoXML.Element("title").Value; return blogInfo; }
// 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; }
// 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 information from the XML data via XMLDocument, // and set it in the returned blog info object private static BlogInfo getBlogInfoXMLDoc(XmlDocument xmlData) { var blogInfo = new BlogInfo(); // Get the blog information XML node XmlNode blogInfoXMLNode = xmlData.SelectSingleNode(Constants.BlogInfoXMLNode); // Set the blog information in the blog info object blogInfo.Title = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLTitleIndex).InnerText; blogInfo.Description = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLDescriptionIndex).InnerText; blogInfo.Link = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLLinkIndex).InnerText; /* string blogInfoXMLNodeName = blogInfoXMLNode.Name; string blogInfoTitleXMLNodeName = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLTitleIndex).Name; string blogInfoDescrXMLNodeName = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLDescriptionIndex).Name; string blogInfoLinkXMLNodeName = blogInfoXMLNode.ChildNodes.Item(Constants.BlogInfoXMLLinkIndex).Name; */ return blogInfo; }