Exemplo n.º 1
0
        internal static Bundle Load(XmlReader reader)
        {
            XElement feed;

            var settings = new XmlReaderSettings();

            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace             = true;

            try
            {
                var internalReader = XmlReader.Create(reader, settings);
                feed = XDocument.Load(internalReader, LoadOptions.SetLineInfo).Root;
                if (feed.Name != XNamespace.Get(ATOMPUB_NS) + "feed")
                {
                    throw Error.Format("Input data is not an Atom feed", null);
                }
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while loading feed: " + exc.Message, null);
            }

            Bundle result;

            try
            {
                result = new Bundle()
                {
                    Title       = SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_TITLE)),
                    LastUpdated = SerializationUtil.InstantOrNull(feed.Element(XATOMNS + XATOM_UPDATED)),
                    Id          = SerializationUtil.UriValueOrNull(feed.Element(XATOMNS + XATOM_ID)),
                    Links       = getLinks(feed.Elements(XATOMNS + XATOM_LINK)),
                    Tags        = TagListParser.ParseTags(feed.Elements(XATOMNS + XATOM_CATEGORY)),
                    AuthorName  = feed.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                  SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_AUTHOR)
                                                                      .Element(XATOMNS + XATOM_AUTH_NAME)),
                    AuthorUri = feed.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_AUTHOR)
                                                                    .Element(XATOMNS + XATOM_AUTH_URI)),
                    TotalResults = SerializationUtil.IntValueOrNull(feed.Element(XOPENSEARCHNS + XATOM_TOTALRESULTS))
                };
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while parsing xml feed attributes: " + exc.Message, null);
            }

            result.Entries = loadEntries(feed.Elements().Where(elem =>
                                                               (elem.Name == XATOMNS + XATOM_ENTRY ||
                                                                elem.Name == XTOMBSTONE + XATOM_DELETED_ENTRY)), result);

            return(result);
        }