public void ProcessFeed() { WebRequest request = WebRequest.Create(_feedUrl); using (var response = request.GetResponse()) { var xmlReader = XmlReader.Create(response.GetResponseStream()); XDocument document = XDocument.Load(xmlReader); IEnumerable <Article> articles = document.Descendants("channel") .Elements("item") .Select(i => new Article { Title = i.Element("title").Value, Link = i.Element("link").Value, Category = i.Element("category")?.Value, Description = i.Element("Description")?.Value ?? i.Element("description").Value, PublicationDate = DateTime.ParseExact(Rfc822DateTime.ConvertZoneToLocalDifferential(i.Element("pubDate").Value), Rfc822DateTime.Rfc822DateTimePatterns, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal) }); foreach (var article in articles) { Runner.UpdateCount(); string querry = SqlQuerry.sqlRequest; using (SqlCommand updSql = new SqlCommand(querry, con)) { updSql.Parameters.AddWithValue("@Title", article.Title); updSql.Parameters.AddWithValue("@Link", article.Link); updSql.Parameters.AddWithValue("@Category", article.Category); updSql.Parameters.AddWithValue("@Description", article.Description); updSql.Parameters.AddWithValue("@PublicationDate", article.PublicationDate); con.Open(); updSql.ExecuteNonQuery(); con.Close(); } } if (_feedUrl == "http://habrahabr.ru/rss/") { Console.WriteLine($"Прочитано новостей с habr.com: {Runner.ArticleCount}"); Runner.ArticleCount = 0; } if (_feedUrl == "http://www.interfax.by/news/feed/") { Console.WriteLine($"Прочитано новостей с interfax.by: {Runner.ArticleCount}"); Runner.ArticleCount = 0; } } }
/// <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); }