【问题标题】:Read ReadElementExtensions from RSS ATOM从 RSS ATOM 读取 ReadElementExtensions
【发布时间】:2015-12-15 19:16:04
【问题描述】:

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

这是我的代码:

RSS 提要的网址: 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; }
}

【问题讨论】:

    标签: c# .net serialization rss atom-feed


    【解决方案1】:

    我在使用 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      相关资源
      最近更新 更多