简体   繁体   English

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

[英]How to read the attribute of an element xml in C# Windows Phone?

I developing an app that can read rss, I set a class like that 我开发了一个可以读取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, the item returns that 但是,该项目返回

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

So, it is clearly that the "enclosure" element return empty string, so how to read the "url" attribute of this "enclosure" tag by giving the annotation like above class, please help me! 因此,很明显,“ enclosure”元素返回空字符串,因此,如何像上面的类一样通过注释来读取“ enclosure”标签的“ url”属性,请帮帮我!

You have to write separate class for enclosure 您必须为外壳编写单独的类

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


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM