// read

        void ReadXml(XmlReader reader, bool fromSerializable)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            SetFeed(CreateFeedInstance());

            reader.MoveToContent();

            string ver = reader.GetAttribute("version");

            if (ver != "2.0")
            {
                throw new NotSupportedException(String.Format("RSS Version '{0}' is not supported", ver));
            }

            if (PreserveAttributeExtensions && reader.MoveToFirstAttribute())
            {
                do
                {
                    if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
                    {
                        continue;
                    }
                    if (reader.NamespaceURI == String.Empty && reader.LocalName == "version")
                    {
                        continue;
                    }
                    if (!TryParseAttribute(reader.LocalName, reader.NamespaceURI, reader.Value, Feed, Version))
                    {
                        Feed.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                    }
                }while (reader.MoveToNextAttribute());
            }

            reader.ReadStartElement();                        // <rss> => <channel>
            reader.MoveToContent();
            reader.ReadStartElement("channel", String.Empty); // <channel> => *

            Collection <SyndicationItem> items = null;

            for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    throw new XmlException("Only element node is expected under 'channel' element");
                }
                if (reader.NamespaceURI == String.Empty)
                {
                    switch (reader.LocalName)
                    {
                    case "title":
                        Feed.Title = ReadTextSyndicationContent(reader);
                        continue;

                    case "link":
                        SyndicationLink l = Feed.CreateLink();
                        ReadLink(reader, l);
                        Feed.Links.Add(l);
                        continue;

                    case "description":
                        Feed.Description = ReadTextSyndicationContent(reader);
                        continue;

                    case "language":
                        Feed.Language = reader.ReadElementContentAsString();
                        continue;

                    case "copyright":
                        Feed.Copyright = ReadTextSyndicationContent(reader);
                        continue;

                    case "managingEditor":
                        SyndicationPerson p = Feed.CreatePerson();
                        ReadPerson(reader, p);
                        Feed.Authors.Add(p);
                        continue;

                    case "pubDate":
                        // FIXME: somehow DateTimeOffset causes the runtime crash.
                        reader.ReadElementContentAsString();
                        // Feed.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
                        continue;

                    case "lastBuildDate":
                        // FIXME: somehow DateTimeOffset causes the runtime crash.
                        reader.ReadElementContentAsString();
                        // Feed.LastUpdatedTime = FromRFC822DateString (reader.ReadElementContentAsString ());
                        continue;

                    case "category":
                        SyndicationCategory c = Feed.CreateCategory();
                        ReadCategory(reader, c);
                        Feed.Categories.Add(c);
                        continue;

                    case "generator":
                        Feed.Generator = reader.ReadElementContentAsString();
                        continue;

                    //  "webMaster" "docs" "cloud" "ttl" "image" "rating" "textInput" "skipHours" "skipDays" are not handled.
                    case "item":
                        if (items == null)
                        {
                            items      = new Collection <SyndicationItem> ();
                            Feed.Items = items;
                        }
                        items.Add(ReadItem(reader, Feed));
                        continue;
                    }
                }
                if (!TryParseElement(reader, Feed, Version))
                {
                    if (PreserveElementExtensions)
                    {
                        // FIXME: what to specify for maxExtensionSize
                        LoadElementExtensions(reader, Feed, int.MaxValue);
                    }
                    else
                    {
                        reader.Skip();
                    }
                }
            }

            reader.ReadEndElement(); // </channel>
            reader.ReadEndElement(); // </rss>
        }
예제 #2
0
        // read

        void ReadXml(XmlReader reader, bool fromSerializable)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            SetFeed(CreateFeedInstance());

            reader.MoveToContent();

            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
                    {
                        continue;
                    }
                    if (reader.LocalName == "lang" && reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace")
                    {
                        Feed.Language = reader.Value;
                        continue;
                    }
                    if (!TryParseAttribute(reader.LocalName, reader.NamespaceURI, reader.Value, Feed, Version) && PreserveAttributeExtensions)
                    {
                        Feed.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                    }
                } while (reader.MoveToNextAttribute());
            }

            reader.ReadStartElement();

            Collection <SyndicationItem> items = null;

            for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    throw new XmlException("Only element node is expected under 'feed' element");
                }
                if (reader.NamespaceURI == AtomNamespace)
                {
                    switch (reader.LocalName)
                    {
                    case "author":
                        SyndicationPerson p = Feed.CreatePerson();
                        ReadPerson(reader, p);
                        Feed.Authors.Add(p);
                        continue;

                    case "category":
                        SyndicationCategory c = Feed.CreateCategory();
                        ReadCategory(reader, c);
                        Feed.Categories.Add(c);
                        continue;

                    case "contributor":
                        p = Feed.CreatePerson();
                        ReadPerson(reader, p);
                        Feed.Contributors.Add(p);
                        continue;

                    case "generator":
                        Feed.Generator = reader.ReadElementContentAsString();
                        continue;

                    // "icon" is an extension
                    case "id":
                        Feed.Generator = reader.ReadElementContentAsString();
                        continue;

                    case "link":
                        SyndicationLink l = Feed.CreateLink();
                        ReadLink(reader, l);
                        Feed.Links.Add(l);
                        continue;

                    case "logo":
                        Feed.ImageUrl = CreateUri(reader.ReadElementContentAsString());
                        continue;

                    case "rights":
                        Feed.Copyright = ReadTextSyndicationContent(reader);
                        continue;

                    case "subtitle":
                        Feed.Description = ReadTextSyndicationContent(reader);
                        continue;

                    case "title":
                        Feed.Title = ReadTextSyndicationContent(reader);
                        continue;

                    case "updated":
                        // FIXME: somehow DateTimeOffset causes the runtime crash.
                        reader.ReadElementContentAsString();
                        // Feed.LastUpdatedTime = XmlConvert.ToDateTimeOffset (reader.ReadElementContentAsString ());
                        continue;

                    case "entry":
                        if (items == null)
                        {
                            items      = new Collection <SyndicationItem> ();
                            Feed.Items = items;
                        }
                        items.Add(ReadItem(reader, Feed));
                        continue;
                    }
                }
                if (!TryParseElement(reader, Feed, Version))
                {
                    if (PreserveElementExtensions)
                    {
                        // FIXME: what to specify for maxExtensionSize
                        LoadElementExtensions(reader, Feed, int.MaxValue);
                    }
                    else
                    {
                        reader.Skip();
                    }
                }
            }

            reader.ReadEndElement();
        }