Пример #1
0
        public override StructDoc _GetSDoc(string str_xml)
        {
            try
            {
                StructDoc doc = new StructDoc();

                byte[]       byteArray = Encoding.UTF8.GetBytes(str_xml);
                MemoryStream stream    = new MemoryStream(byteArray);

                XDocument docXml = XDocument.Load(stream);

                var rss_ch  = docXml.Element("rss");
                var channal = rss_ch.Element("channel");

                doc.Title    = channal.Element("title").Value;
                doc.MainLink = channal.Element("link").Value;
                doc.dataDoc  = DateTime.Parse(channal.Element("lastBuildDate").Value);

                var docItems = channal.Elements("item").ToList();
                foreach (var item in docItems)
                {
                    StructItemDoc structItemDoc = new StructItemDoc();
                    structItemDoc.Title        = item.Element("title").Value;
                    structItemDoc.guid         = item.Element("guid").Value;
                    structItemDoc.link         = item.Element("link").Value;
                    structItemDoc.discription  = item.Element("description").Value;
                    structItemDoc.ItemDateTime = DateTime.Parse(item.Element("pubDate").Value);
                    //structItemDoc.Creator = item.Element("dc:creator").Value;

                    var cat_s = item.Elements("category").ToList();
                    foreach (var category in cat_s)
                    {
                        structItemDoc.Categori.Add(category.Value);
                    }
                    doc.LItemDoc.Add(structItemDoc);
                }
                return(doc);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }
Пример #2
0
        public async void UpdateData(StructDoc structDoc)
        {
            try
            {
                DDB dB = new DDB();

                var Lss = dB.site_source.Where(t => t.NameSite == structDoc.MainLink).ToList();
                if (!Lss.Any())
                {
                    dB.site_source.Add(new site_source()
                    {
                        NameSite = structDoc.MainLink
                    });
                    await dB.SaveChangesAsync();
                }
                int ine = 0;
                foreach (var item in structDoc.LItemDoc)
                {
                    var a1 = dB.NewsOnSites.Where(t => t.C_guid == item.guid).Any();
                    if (!a1)
                    {
                        NewsOnSites newsOn = new NewsOnSites();
                        newsOn.Title       = item.Title;
                        newsOn.pudData     = item.ItemDateTime;
                        newsOn.link        = item.link;
                        newsOn.discription = item.discription;
                        newsOn.C_guid      = item.guid;
                        dB.NewsOnSites.Add(newsOn);
                        ine += await dB.SaveChangesAsync();

                        foreach (var cat in item.Categori)
                        {
                            var         one_cat = dB.category.Where(t => t.NameCategory == cat).ToList();
                            site_source site    = new site_source();
                            site = dB.site_source.Where(t => t.NameSite == structDoc.MainLink).First();
                            if (!one_cat.Any())
                            {
                                dB.category.Add(new category()
                                {
                                    NameCategory = cat, SiteWithCategory = site.IdTable
                                });
                                await dB.SaveChangesAsync();
                            }

                            var idNOS = dB.NewsOnSites.Where(t => t.C_guid == item.guid).First().C_guid;
                            var idCat = dB.category.Where(t => t.NameCategory == cat && t.SiteWithCategory == site.IdTable).First().IdTable;

                            dB.NewsAndCategory.Add(new NewsAndCategory()
                            {
                                IdCategory = idCat, IdNews = idNOS
                            });
                            await dB.SaveChangesAsync();
                        }
                    }
                }
                Console.WriteLine(structDoc.MainLink);
                Console.WriteLine("Прочитанно новочтей: " + structDoc.LItemDoc.Count);
                Console.WriteLine("Сохранено новостей: " + ine);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }