Read ReadElementExtensions from RSS ATOM

痞子三分冷 提交于 2019-12-12 05:12:26

问题


I have the following code and I cannot deserialize a RSS ATOM feed to a class. The "team" node cannot be read and the rest of the properties don't seem to be getting filled. When I comment out the "team" node.

This is my code:

The URL of the RSS feed: 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; }
}

回答1:


I had similar problems with using the ReadElementExtension method and with reading custom attributes. I solved this by using XElement as the return type.

Here's how I handled it (using C# 6.0 constructs):

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;
}

Here's some example code for calling these methods, assuming that "feed" is an instantiated SyndicationFeed object. These read custom elements and attributes for ITunes podcasts:

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;


来源:https://stackoverflow.com/questions/34297550/read-readelementextensions-from-rss-atom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!