使用xml序列化程序解析rss feed

时间:2014-10-21 06:52:51

标签: windows-phone-8

有人可以告诉我如何解析这个Url

http://bitcast-r.v1.sjc1.bitgravity.com/objectinfo/MIB/radio/inbradio_play.xml

这样所有Feed中除第一项以外的所有项目都应该显示在listbox中。我无法理解如何解析省略第一项。 这是我用于解析的代码。但在这种情况下,我得到了所有Feed的项目。 但我不需要得到第一项(即)torilive.How我可以解析这样我不应该得到第一项

MainPage.xaml.cs中:

 public MainPage()
    {
        InitializeComponent();
        // is there network connection available


        if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            MessageBox.Show("No network connection available!");
            return;
        }
        // start loading XML-data
        WebClient downloader = new WebClient();
        Uri uri = new Uri("http://bitcast-r.v1.sjc1.bitgravity.com/objectinfo/MIB/radio/inbradio_play.xml", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ChannelDownloaded);
        downloader.DownloadStringAsync(uri);
    }

    void ChannelDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the XML-file!");
        }
        else
        {
            // Deserialize if download succeeds
            XmlSerializer serializer = new XmlSerializer(typeof(Channel));
            XDocument document = XDocument.Parse(e.Result);
           Channel channel = (Channel)serializer.Deserialize(document.CreateReader());
            listBox.ItemsSource = channel.Collection;
        }
    }

Channel.cs:

    namespace Sample
    {
        [XmlRoot("rss")]
        public class Channel
    {
        [XmlArray("channel")]
        XmlArrayItem("item")]
        public ObservableCollection<Items> Collection { get; set; }
    }
 }

Items.cs:

namespace Sample
{
    public class Items
    {
        [XmlElement("title")]
        public string title { get; set; }

        [XmlElement("link")]
        public string link { get; set; }

         [XmlElement("image")]
         public string image { get; set; }
    }
 }

希望有人帮忙。很多人提前感谢。

1 个答案:

答案 0 :(得分:0)

好节目:)


C#

void ChannelDownloaded(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Result == null || e.Error != null)
    {
        MessageBox.Show("There was an error downloading the XML-file!");
    }
    else
    {
        // Deserialize if download succeeds
        XmlSerializer serializer = new XmlSerializer(typeof(Channel));
        XDocument document = XDocument.Parse(e.Result);
        Channel channel = (Channel)serializer.Deserialize(document.CreateReader());


        // remove the first item
        if (channel.Collection.Count > 0)
            channel.Collection.RemoveAt(0);

        this.myListBox.ItemsSource = channel.Collection;            
   }
}

XAML

<ListBox x:Name="myListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,1" BorderBrush="Red">
            <StackPanel>
                <TextBlock Text="{Binding title}"></TextBlock>
                <TextBlock Text="{Binding link}" Width="350"></TextBlock>
                <Image Source="{Binding image}" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"></Image>
            </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

代码在行动:

enter image description here


您可以修改模板以获得所需的结果。