
Xml Hydration
By: a guest on
Aug 20th, 2012 | syntax:
C# | size: 1.68 KB | hits: 21 | expires: Never
/// <summary>Converts XML to Post models</summary>
/// <param name="xml">Xml string to parse</param>
/// <returns>Returns an enumerable of the Post model</returns>
public IEnumerable<Post> XmlToPost(string xml)
{
if (string.IsNullOrEmpty(xml))
{
//// Return nothing
return new Post[] { }.AsEnumerable();
}
else
{
var doc = XDocument.Parse(xml);
var posts = (from g in doc.Element("posts").Elements("post")
select new Post()
{
Id = int.Parse(g.Element("id").Value),
Title = g.Element("title").Value,
Photos = int.Parse(g.Element("photos").Value),
Comments = int.Parse(g.Element("comments").Value),
GoodCount = int.Parse(g.Element("goodCount").Value),
BadCount = int.Parse(g.Element("badCount").Value),
PostType = g.Element("type").Value,
Url = g.Element("web_url").Value,
SmallThumbUrl = g.Element("small_url").Value,
MediumThumbUrl = g.Element("thumb_url").Value,
LargeThumbUrl = g.Element("large_url").Value,
DatePosted = DateTime.Parse(g.Element("time").Value)
}).ToArray();
return posts.AsEnumerable();
}
}