/// <summary> /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent. /// </summary> /// <param name="s">A string containing a date and time to convert.</param> /// <param name="result"> /// When this method returns, contains the <see cref="DateTime"/> value equivalent to the date and time /// contained in <paramref name="s"/>, expressed as <i>Coordinated Universal Time (UTC)</i>, /// if the conversion succeeded, or <see cref="DateTime.MinValue">MinValue</see> if the conversion failed. /// The conversion fails if the s parameter is a <b>null</b> reference (Nothing in Visual Basic), /// or does not contain a valid string representation of a date and time. /// This parameter is passed uninitialized. /// </param> /// <returns><b>true</b> if the <paramref name="s"/> parameter was converted successfully; otherwise, <b>false</b>.</returns> /// <remarks> /// The string <paramref name="s"/> is parsed using formatting information in the <see cref="DateTimeFormatInfo.InvariantInfo"/> object. /// </remarks> public static bool TryParse(string s, out DateTime result) { //------------------------------------------------------------ // Attempt to convert string representation //------------------------------------------------------------ bool wasConverted = false; result = DateTime.MinValue; if (!String.IsNullOrEmpty(s)) { DateTime parseResult; if (DateTime.TryParseExact(Rfc822DateTime.ConvertZoneToLocalDifferential(s), Rfc822DateTime.Rfc822DateTimePatterns, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out parseResult)) { result = DateTime.SpecifyKind(parseResult, DateTimeKind.Utc); wasConverted = true; } } return(wasConverted); }
/// <summary> /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent. /// </summary> /// <param name="s">A string containing a date and time to convert.</param> /// <returns> /// A <see cref="DateTime"/> equivalent to the date and time contained in <paramref name="s"/>, /// expressed as <i>Coordinated Universal Time (UTC)</i>. /// </returns> /// <remarks> /// The string <paramref name="s"/> is parsed using formatting information in the <see cref="DateTimeFormatInfo.InvariantInfo"/> object. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="s"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException"><paramref name="s"/> is an empty string.</exception> /// <exception cref="FormatException"><paramref name="s"/> does not contain a valid RFC 822 string representation of a date and time.</exception> public static DateTime Parse(string s) { DateTime result; if (Rfc822DateTime.TryParse(s, out result)) { return(result); } else { throw new FormatException(String.Format(null, "{0} is not a valid RFC 822 string representation of a date and time.", s)); } }
public FeedInfo Parse(XmlReader reader) { try { FeedInfo info = new FeedInfo(); //create a List of type Dictionary<string,string> for the element names and values var items = new List <Dictionary <string, string> >(); // declare a Dictionary to capture each current Item in the while loop Dictionary <string, string> currentItem = null; bool parsingItems = false; /// Read each element with the reader while (reader.Read()) { // if it's an element, we want to process it if (reader.NodeType == XmlNodeType.Element) { string name = reader.Name; // rss1.0 - channel/title // rss2.0 - channel/title // atom0.3 - feed/title // atom1.0 - feed/title if (!parsingItems && name.ToLowerInvariant() == "title") { reader.Read(); info.ActualTitle = reader.Value; } if (name.ToLowerInvariant() == "item" || name.ToLowerInvariant() == "entry") { parsingItems = true; // Save previous item if (currentItem != null) { items.Add(currentItem); } // Create new item currentItem = new Dictionary <string, string>(); } else if (currentItem != null) { reader.Read(); // some feeds can have duplicate keys, so we don't want to blow up here: if (!currentItem.ContainsKey(name)) { currentItem.Add(name, reader.Value.Trim()); } } } } // Save previous item if (currentItem != null) { items.Add(currentItem); } // now create a List of type GenericFeedItem var itemList = new List <FeedItem>(); // iterate all our items from the reader foreach (var d in items) { var itm = new FeedItem(); //do a switch on the Key of the Dictionary <string, string> of each item foreach (string k in d.Keys) { switch (k) { case "title": itm.Title = d[k]; break; case "link": itm.Link = d[k]; break; case "published": case "pubDate": case "issued": DateTime dt; bool ok = Rfc822DateTime.TryParse(d[k], out dt); itm.PubDate = ok ? dt : DateTime.Now; break; case "content": case "description": itm.Description = d[k]; break; default: break; } } // add the created item to our List itemList.Add(itm); } info.Items = itemList; return(info); } catch { return(null); } }