2

我想从这个 URL 中提取数据:http ://rss.cnn.com/rss/edition.rss

所以每个项目看起来像这样:

      <item>
         <title><![CDATA[Ireland stuns England at home of cricket]]></title>
         <description><![CDATA[From World Cup glory to utter humiliation in the space of 10 days.]]></description>
         <link>https://www.cnn.com/2019/07/24/sport/england-ireland-cricket-spt-intl/index.html</link>
         <guid isPermaLink="true">https://www.cnn.com/2019/07/24/sport/england-ireland-cricket-spt-intl/index.html</guid>
         <pubDate>Wed, 24 Jul 2019 13:17:56 GMT</pubDate>
         <media:group>
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-super-169.jpg" height="619" width="1100" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-large-11.jpg" height="300" width="300" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-vertical-large-gallery.jpg" height="552" width="414" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-video-synd-2.jpg" height="480" width="640" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-live-video.jpg" height="324" width="576" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-t1-main.jpg" height="250" width="250" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-vertical-gallery.jpg" height="360" width="270" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-story-body.jpg" height="169" width="300" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-t1-main.jpg" height="250" width="250" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-assign.jpg" height="186" width="248" />
            <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/190724131447-england-ireland-tease-01-hp-video.jpg" height="144" width="256" />
         </media:group>
      </item>

所以我在互联网上找到了一些应该注意此类情况的类,例如:SyndicationFeed 或 XDocument.Parse

所以我尝试了这个:

XmlReader reader = XmlReader.Create(urle);
            SyndicationFeed feeds = SyndicationFeed.Load(reader); // References -> Right Click -> Add Reference -> System.ServiceModel
            reader.Close();

            foreach (SyndicationItem item in feeds.Items)
            {
                string subject = item.Title.Text;
                Console.WriteLine("subject: " + subject);
                if (item.Summary != null)
                {
                    string summary = item.Summary.Text;
                    Console.WriteLine("desc: " + summary);

                }


            }

它与 Title 和 Summary 配合得很好,但它具有处理图像的功能,例如,我如何使用 SyndicationFeed 来做到这一点?

4

1 回答 1

3

及其内容<media:group>被视为扩展元素。

该类SyndicationFeed具有ElementExtensions解决这些问题的属性,
并具有ReadElementExtensions读取和解析它们的方法。

创建一个与<media:group>xml 元素匹配的类。

[XmlRoot("group", Namespace = "http://search.yahoo.com/mrss/")]
public class MediaGroup
{   
    [XmlElement(ElementName = "content", Namespace = "http://search.yahoo.com/mrss/")]
    public MediaContent[] Items { get; set;}
}

还要为项目创建一个类定义<media:content>

public class MediaContent
{
    [XmlAttribute("url")]
    public string Url {  get; set; }

    [XmlAttribute("height")]
    public int Height {  get; set; }

    [XmlAttribute("width")]
    public int Width {  get; set; }

    [XmlAttribute("medium")]
    public string Medium { get; set; }
}

使用XmlSerializer如下所示的读取和解析它们。

var serializer = new XmlSerializer(typeof(MediaGroup));
var mediaGroup = item.ElementExtensions
    .ReadElementExtensions<MediaGroup>("group", "http://search.yahoo.com/mrss/", serializer)
    .FirstOrDefault();
if (mediaGroup != null)
{
    foreach (var mediaItem in mediaGroup.Items)
    {
        Console.WriteLine(mediaItem.Url);
    }
}

完整代码:

using (var reader = XmlReader.Create(urle))
{
    var feeds = SyndicationFeed.Load(reader);

    var serializer = new XmlSerializer(typeof(MediaGroup));

    foreach (SyndicationItem item in feeds.Items)
    {
        string subject = item.Title.Text;
        Console.WriteLine("subject: " + subject);

        var mediaGroup = item.ElementExtensions
            .ReadElementExtensions<MediaGroup>("group", "http://search.yahoo.com/mrss/", serializer)
            .FirstOrDefault();
        if (mediaGroup != null)
        {
            foreach (var mediaItem in mediaGroup.Items)
            {
                Console.WriteLine(mediaItem.Url);
            }
        }                    
    }
}
于 2019-07-25T21:44:35.010 回答