/// <summary> /// transforms XML into IList of SavedSearch /// </summary> /// <param name="responseXml">xml with Twitter response</param> /// <returns>List of SavedSearch</returns> public List <T> ProcessResults(string responseXml) { XElement twitterResponse = XElement.Parse(responseXml); List <XElement> responseItems = new List <XElement>(); // place_type under root means that it's an ID query if (twitterResponse.Element("place_type") != null) { responseItems.Add( new XElement("root", new XElement("contained_within", new XElement("item", twitterResponse.Elements())))); } else // reverse geocode query { responseItems = twitterResponse .Element("result") .Element("places") .Elements("item").ToList(); } var geo = new Geo { Type = Type, Accuracy = Accuracy, Granularity = Granularity, ID = ID, Latitude = Latitude, Longitude = Longitude, IP = IP, MaxResults = MaxResults, Query = Query, ContainedWithin = ContainedWithin, Attribute = Attribute, Places = (from pl in responseItems select Place.CreatePlace(pl.Element("contained_within").Element("item"))) .ToList() }; return(new List <Geo> { geo }.OfType <T>().ToList()); }
/// <summary> /// Shreds an XML element into a Status object /// </summary> /// <param name="status">XML element with info</param> /// <returns>Newly populated status object</returns> public static Status CreateStatus(XElement status) { if (status == null) { return(null); } var createdAtDate = status.Element("created_at") == null || status.Element("created_at").Value == string.Empty ? DateTime.MinValue : DateTime.ParseExact( status.Element("created_at").Value, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); var favorite = status.Element("favorited") == null || status.Element("favorited").Value == string.Empty ? "false" : status.Element("favorited").Value; var user = status.Element("user"); var retweet = status.Element("retweeted_status"); var retweetCount = status.Element("retweet_count") == null || status.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(status.Element("retweet_count").Value.TrimEnd('+')); var retweeted = status.Element("retweeted") == null || status.Element("retweeted").Value == string.Empty ? false : bool.Parse(status.Element("retweeted").Value); var retweetDate = retweet == null ? null : retweet.Element("created_at").Value; var retweetedAtDate = String.IsNullOrEmpty(retweetDate) ? DateTime.MinValue : DateTime.ParseExact( retweetDate, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); List <Contributor> contributors = null; XElement contributorElement = status.Element("contributors"); if (contributorElement != null) { if (contributorElement.Elements("user").Count() > 0) { contributors = (from contr in contributorElement.Elements("user") select new Contributor { ID = contr.Element("id").Value, ScreenName = contr.Element("screen_name").Value }) .ToList(); } else { contributors = (from id in contributorElement.Elements("user_id") select new Contributor { ID = id.Value, ScreenName = string.Empty }) .ToList(); } } XNamespace geoRss = "http://www.georss.org/georss"; var geoStr = status.Element("geo") != null && status.Element("geo").Element(geoRss + "point") != null? status.Element("geo").Element(geoRss + "point").Value: string.Empty; Geo geo = new Geo(); if (!string.IsNullOrEmpty(geoStr)) { var coordArr = geoStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { geo = new Geo { Latitude = tempLatitude, Longitude = tempLongitide }; } } var coordStr = status.Element("coordinates") != null && status.Element("coordinates").Element(geoRss + "point") != null? status.Element("coordinates").Element(geoRss + "point").Value: string.Empty; Coordinate coord = new Coordinate(); if (!string.IsNullOrEmpty(coordStr)) { var coordArr = coordStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { coord = new Coordinate { Latitude = tempLatitude, Longitude = tempLongitide }; } } var place = Place.CreatePlace(status.Element("place")); var annotation = Annotation.CreateAnnotation(status.Element("annotation")); var entities = Entities.CreateEntities(status.Element("entities")); var newStatus = new Status { CreatedAt = createdAtDate, Favorited = bool.Parse(favorite), StatusID = status.GetString("id"), InReplyToStatusID = status.GetString("in_reply_to_status_id"), InReplyToUserID = status.GetString("in_reply_to_user_id"), Source = status.GetString("source"), Text = status.GetString("text"), Truncated = status.GetBool("truncated"), InReplyToScreenName = status.GetString("in_reply_to_screen_name"), Contributors = contributors, Geo = geo, Coordinates = coord, Place = place, Annotation = annotation, User = User.CreateUser(user), Entities = entities, Retweeted = retweeted, RetweetCount = retweetCount, Retweet = retweet == null ? null : new Retweet { ID = retweet.GetString("id"), CreatedAt = retweetedAtDate, Favorited = retweet.GetBool("favorited"), InReplyToScreenName = retweet.GetString("in_reply_to_screen_name"), InReplyToStatusID = retweet.GetString("in_reply_to_status_id"), InReplyToUserID = retweet.GetString("in_reply_to_user_id"), Source = retweet.GetString("source"), Text = retweet.GetString("text"), Retweeted = retweet.GetBool("retweeted"), RetweetCount = //retweet.GetInt("retweet_count"), retweet.Element("retweet_count") == null || retweet.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(retweet.Element("retweet_count").Value.TrimEnd('+')), Truncated = retweet.GetBool("truncated", true), RetweetingUser = User.CreateUser(retweet.Element("user")) } }; return(newStatus); }