Example #1
0
        /// <summary>
        /// Parses the supplied XML into an instance of
        /// <see cref="XmlAtomContent" />.
        /// </summary>
        /// <param name="xml">
        /// A string of characters containing the XML representation of an Atom
        /// Content element.
        /// </param>
        /// <param name="serializer">
        /// The <see cref="IContentSerializer" /> used to deserialize the XML
        /// into an instance of the correct object type.
        /// </param>
        /// <returns>
        /// A new instance of <see cref="XmlAtomContent" /> containing the data
        /// from the supplied <paramref name="xml" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serializer" /> is <see langword="null" />.
        /// </exception>
        public static XmlAtomContent Parse(
            string xml,
            IContentSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            var sr = new StringReader(xml);

            try
            {
                using (var r = XmlReader.Create(sr))
                {
                    sr = null;
                    return(XmlAtomContent.ReadFrom(r, serializer));
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Dispose();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Creates an <see cref="AtomEntry" /> instance from XML.
        /// </summary>
        /// <param name="xmlReader">
        /// The <see cref="XmlReader" /> containing the XML representation of
        /// the Atom Entry.
        /// </param>
        /// <param name="serializer">
        /// The <see cref="IContentSerializer" /> used to serialize custom XML
        /// content.
        /// </param>
        /// <returns>
        /// A new instance of <see cref="AtomEntry" /> containing the data from
        /// the XML representation of the Atom Entry contained in
        /// <paramref name="xmlReader" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serializer" /> is <see langword="null" />.
        /// </exception>
        public static AtomEntry ReadFrom(
            XmlReader xmlReader,
            IContentSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            var navigator = new XPathDocument(xmlReader).CreateNavigator();

            var resolver = new XmlNamespaceManager(new NameTable());

            resolver.AddNamespace("atom", "http://www.w3.org/2005/Atom");

            var id = navigator
                     .Select("/atom:entry/atom:id", resolver).Cast <XPathNavigator>()
                     .Single().Value;
            var title = navigator
                        .Select("/atom:entry/atom:title[@type = 'text']", resolver).Cast <XPathNavigator>()
                        .Single().Value;
            var published = navigator
                            .Select("/atom:entry/atom:published", resolver).Cast <XPathNavigator>()
                            .Single().Value;
            var updated = navigator
                          .Select("/atom:entry/atom:updated", resolver).Cast <XPathNavigator>()
                          .Single().Value;
            var author = navigator
                         .Select("/atom:entry/atom:author", resolver).Cast <XPathNavigator>()
                         .Single().ReadSubtree();
            var content = navigator
                          .Select("/atom:entry/atom:content[@type = 'application/xml']", resolver).Cast <XPathNavigator>()
                          .Single().ReadSubtree();
            var links = navigator
                        .Select("/atom:entry/atom:link", resolver).Cast <XPathNavigator>();

            return(new AtomEntry(
                       UuidIri.Parse(id),
                       title,
                       DateTimeOffset.Parse(published, CultureInfo.InvariantCulture),
                       DateTimeOffset.Parse(updated, CultureInfo.InvariantCulture),
                       AtomAuthor.ReadFrom(author),
                       XmlAtomContent.ReadFrom(content, serializer),
                       links.Select(x => AtomLink.ReadFrom(x.ReadSubtree()))));
        }