从RSS ATOM中读取ReadElementExtensions

本文关键字:读取 ReadElementExtensions ATOM RSS | 更新日期: 2023-09-27 18:26:58

我有以下代码,无法将RSS ATOM提要反序列化为类。"团队"节点无法读取,并且其他属性似乎没有被填充。当我注释掉"团队"节点时。

这是我的代码:

RSS源的URL:http://www.volleybal.nl/application/handlers/export.php?format=rss&type=poule&standen=D3K&iRegionId=7000

    public static SyndicationFeed GetFeed(string feedUrl)
    {
        Uri serviceUri = new Uri(feedUrl);
        WebClient downloader = new WebClient();
        Stream responseStream = downloader.OpenRead(serviceUri);
        XmlReader responseReader = XmlReader.Create(responseStream);
        SyndicationFeed syndicationFeed = SyndicationFeed.Load(responseReader);
        return syndicationFeed;
    }
System.Collections.ObjectModel.Collection<Stand> oo = programFeed.ElementExtensions.ReadElementExtensions<Stand>("ranking", "http://www.nevobo.nl/competitie/");
[Serializable]
[XmlSerializerFormat]
[DataContract(Name = "stand", Namespace = "http://www.nevobo.nl/competitie/")]
[XmlRoot(ElementName = "stand", Namespace = "http://www.nevobo.nl/competitie/")]
public class Stand
{
    public Stand()
    {
    }
    [DataMember(Name = "nummer")]
    [XmlElement("nummer")]
    public string Nummer { get; set; }
    [DataMember(Name = "team")]
    [XmlElement("team")]
    public Team Team { get; set; }
    [DataMember(Name = "wedstrijden")]
    [XmlElement("wedstrijden")]
    public string Wedstrijden { get; set; }
    [DataMember(Name = "punten")]
    [XmlElement("punten")]
    public string Punten { get; set; }
    [DataMember(Name = "setsvoor")]
    [XmlElement("setsvoor")]
    public string Setsvoor { get; set; }
    [DataMember(Name = "setstegen")]
    [XmlElement("setstegen")]
    public string Setstegen { get; set; }
    [DataMember(Name = "puntenvoor")]
    [XmlElement("puntenvoor")]
    public string Puntenvoor { get; set; }
    [DataMember(Name = "puntentegen")]
    [XmlElement("puntentegen")]
    public string Puntentegen { get; set; }
}
[Serializable]
//[XmlSerializerFormat]
//[DataContract(Name = "team")]
[DataContract(Name = "team", Namespace = "http://www.nevobo.nl/competitie/")]
//[XmlRoot(ElementName = "team", Namespace = "http://www.nevobo.nl/competitie/")]
//[System.Xml.Serialization.XmlType(AnonymousType = true)]
public class Team
{
    public Team()
    {
    }
    [DataMember(Name = "id")]
    [XmlAttribute(AttributeName ="id")]
    public string Id { get; set; }
    [DataMember]
    [XmlText]
    public string Text { get; set; }
}

从RSS ATOM中读取ReadElementExtensions

我在使用ReadElementExtension方法和读取自定义属性时也遇到了类似的问题。我使用XElement作为返回类型来解决这个问题。

以下是我如何处理它(使用C#6.0构造):

private XElement ReadElement(SyndicationFeed feed, string extspace, string extname)
{
    var elements = feed.ElementExtensions.ReadElementExtensions<XElement>(extname, extspace);
    return elements.FirstOrDefault();
}
private XAttribute ReadAttribute(string extspace, string extname, string attname)
{
    var element = ReadElement(extspace, extname);
    var attribute = element?.Attribute(attname);
    return attribute;
}

下面是一些调用这些方法的示例代码,假设"feed"是一个实例化的SyndicationFeed对象。这些阅读ITunes播客的自定义元素和属性:

private const string ITUNES = "http://www.itunes.com/dtds/podcast-1.0.dtd";
...
string explicitValue =  ReadElement(ITUNES, "explicit")?.Value;
string imageUrl = ReadAttribute(ITUNES, "image", "href")?.Value;