Don't like ads? PRO users don't see any ads ;-)
Guest

Xml Hydration

By: a guest on Aug 20th, 2012  |  syntax: C#  |  size: 1.68 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         /// <summary>Converts XML to Post models</summary>
  2.         /// <param name="xml">Xml string to parse</param>
  3.         /// <returns>Returns an enumerable of the Post model</returns>
  4.         public IEnumerable<Post> XmlToPost(string xml)
  5.         {
  6.             if (string.IsNullOrEmpty(xml))
  7.             {
  8.                 //// Return nothing
  9.                 return new Post[] { }.AsEnumerable();
  10.             }
  11.             else
  12.             {
  13.                 var doc = XDocument.Parse(xml);
  14.                 var posts = (from g in doc.Element("posts").Elements("post")
  15.                              select new Post()
  16.                              {
  17.                                  Id = int.Parse(g.Element("id").Value),
  18.                                  Title = g.Element("title").Value,
  19.                                  Photos = int.Parse(g.Element("photos").Value),
  20.                                  Comments = int.Parse(g.Element("comments").Value),
  21.                                  GoodCount = int.Parse(g.Element("goodCount").Value),
  22.                                  BadCount = int.Parse(g.Element("badCount").Value),
  23.                                  PostType = g.Element("type").Value,
  24.                                  Url = g.Element("web_url").Value,
  25.                                  SmallThumbUrl = g.Element("small_url").Value,
  26.                                  MediumThumbUrl = g.Element("thumb_url").Value,
  27.                                  LargeThumbUrl = g.Element("large_url").Value,
  28.                                  DatePosted = DateTime.Parse(g.Element("time").Value)
  29.                              }).ToArray();
  30.                 return posts.AsEnumerable();
  31.             }
  32.         }