如何在C#Windows Phone中读取元素xml的属性?

时间:2016-06-08 10:25:57

标签: c# xml windows-phone-8.1 rss rss-reader

我正在开发一个可以读取rss的应用程序,我设置了类似

的类
public class Item
{
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("description")]
    public string description { get; set; }
    [XmlElement("enclosure")]
    public string enclosure { get; set; }
    [XmlElement("pubDate")]
    public string pubDate { get; set; }
    [XmlElement("link")]
    public string link { get; set; }
}

Howerver,该项目返回

<item>
<title>
Colombia 2-1 Paraguay: James Rodriguez and Carlos Bacca score as Jose Pekerman's side reach Copa America quarter-finals
</title>
<link>
http://www.dailymail.co.uk/sport/football/article-3630657/Colombia-2-1-Paraguay-James-Rodriguez-Carlos-Bacca-score-Jose-Pekerman-s-reach-Copa-America-quarter-finals.html?ITO=1490&ns_mchannel=rss&ns_campaign=1490
</link>
<description>
James Rodriguez scored a goal and set up another as Colombia became the first team to clinch a place in the Copa America quarter-finals. The Real Madrid provided the assist for Carlos Bacca's opener.
</description>
<enclosure url="http://i.dailymail.co.uk/i/pix/2016/06/08/05/350A4D8200000578-0-image-a-63_1465360921756.jpg" type="image/jpeg" length="84507" />
<pubDate>Wed, 08 Jun 2016 06:32:45 +0100</pubDate>
<guid>
http://www.dailymail.co.uk/sport/football/article-3630657/Colombia-2-1-Paraguay-James-Rodriguez-Carlos-Bacca-score-Jose-Pekerman-s-reach-Copa-America-quarter-finals.html?ITO=1490&ns_mchannel=rss&ns_campaign=1490
</guid>
</item>

所以,显然&#34;圈地&#34;元素返回空字符串,所以如何读取&#34; url&#34;这个&#34; enclosure&#34;的属性通过给出类似上面的注释标记,请帮助我!

1 个答案:

答案 0 :(得分:0)

您必须为机箱

编写单独的类
     public class Item
        {
            [XmlElement("title")]
            public string title { get; set; }
            [XmlElement("description")]
            public string description { get; set; }
            [XmlElement("enclosure")]
            public Enclosure enclosure { get; set; }
            [XmlElement("pubDate")]
            public string pubDate { get; set; }
            [XmlElement("link")]
            public string link { get; set; }
        }

    public class Enclosure 
    {
        [XmlAttribute("url")]
        public string Url { get; set; }
    //if enclosure has any child elements it comes under XmlElement like this
    // [XmlElement("enclosurechildelement")]
    // public string enclosurechildelement { get; set; }


    }