Esempio n. 1
0
        public static List<BlogEntry> FillEntriesFromAtom(string xml)
        {
            var entries = new List<BlogEntry>();
            var docTemp = XDocument.Parse(xml);
            docTemp.Declaration = new XDeclaration("1.0", "utf-8", null);
            var doc = new XDocument(docTemp);

            if (doc.Root == null)
                return entries;

            var rawEntries = doc.Root.Descendants(XmlnsAtom + "entry");

            foreach (var rawEntry in rawEntries)
            {
                var entry = new BlogEntry
                {
                    Title = GetXmlValueFromNamespace(rawEntry, XmlnsAtom + "title"),
                    PublishedDate =
                        DateTime.Parse(GetXmlValueFromNamespace(rawEntry, XmlnsAtom + "published"),
                                       new CultureInfo("nb-NO", false)),
                    UpdatedDate = DateTime.Parse(GetXmlValueFromNamespace(rawEntry, XmlnsAtom + "updated"),
                                                 new CultureInfo("nb-NO", false)),
                    Content = GetXmlValueFromNamespace(rawEntry, XmlnsAtom + "content"),
                    AuthorName =
                        GetXmlValueFromNamespace(
                            rawEntry.Descendants(XmlnsAtom.GetName("author")).FirstOrDefault(), XmlnsAtom.GetName("name"))
                };

                // thumbnail
                var xAttribute = rawEntry.Elements(XmlnsThumb.GetName("thumbnail")).Attributes("url").FirstOrDefault();
                if (xAttribute != null)
                    entry.ThumbnailUrl = xAttribute.Value;

                // url
                var link = rawEntry.Elements(XmlnsAtom.GetName("link")).Where(x => x.FirstAttribute.Value.Equals("alternate"));
                var firstOrDefaultHref = link.Attributes("href").FirstOrDefault();

                if (firstOrDefaultHref != null)
                    entry.Url = firstOrDefaultHref.Value;

                // description
                var desc = Regex.Replace(entry.Content, "<(.|\n)*?>", "");

                entry.Description = Regex.Replace(desc, "\\n", "");
                var descLength = DESCRIPTION_LENGTH < entry.Description.Length
                                     ? DESCRIPTION_LENGTH
                                     : entry.Description.Length;

                entry.Description = entry.Description.Substring(0, descLength) + " [...]";

                entries.Add(entry);
            }
            entries = entries.OrderByDescending(x => x.PublishedDate).ToList();
            return entries;

        }
Esempio n. 2
0
        public static List<BlogEntry> FillEntriesFromRss(string xml)
        {
            var entries = new List<BlogEntry>();

            var doc = XDocument.Parse(xml);
            if (doc.Root == null)
                return entries;

            var rawEntries = doc.Root.Descendants("item");
            foreach (var rawEntry in rawEntries)
            {
                var entry = new BlogEntry
                {
                    Title = GetXmlValue(rawEntry, "title"),
                    Url = GetXmlValue(rawEntry, "link"),
                    Content =
                        HttpUtility.HtmlDecode(GetXmlValueFromNamespace(rawEntry,
                                                                        ContentNamespace.GetName(
                                                                            "encoded"))),
                    PublishedDate = DateTime.Parse(GetXmlValue(rawEntry, "pubDate")),
                    Description = GetXmlValue(rawEntry, "description"),
                    AuthorName = GetXmlValueFromNamespace(rawEntry, Author.GetName("creator")),

                };
                //thumbnail
                var imageElements = rawEntry.Elements(Media.GetName("content"));
                var imageContent = imageElements.Attributes("url").FirstOrDefault(x => !string.IsNullOrEmpty(x.Value));
                if (imageContent != null)
                    entry.ThumbnailUrl = imageContent.Value;

                entries.Add(entry);
            }
            return entries;
        }