如何在C#Windows Phone中读取元素xml的属性?
问题描述:
我开发一个应用程序,可以阅读RSS,我设置这样的如何在C#Windows Phone中读取元素xml的属性?
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>
因此,它是明确表示,“圈地”元素返回空字符串,所以如何通过给出像上面类的注释来读取这个“enclosure”标签的“url”属性,请帮助我!
答
你必须编写单独的类外壳
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; }
}