English 中文(简体)
WP7/银星——无法使用Linq和WebClient展示XML元素
原标题:WP7/ Silverlight - Unable to display XML element using Linq and WebClient

我试图从一个XML档案中显示一些内容,该档案存放在遥远的地方,并在一个清单箱中显示。

当我发布消息时,我正在找不到正式例外。 我附上了一个错误的缩略语!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace tvGuide
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

         }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient c = new WebClient();
            c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
            c.DownloadStringAsync(new Uri("http://www.designized.com/tv/freeview.xml?"));
        }

        void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            XElement r = XElement.Parse(e.Result);


            listBox1.ItemsSource = from tv in r.Descendants("programme")
                                   select new TVNZ
                                   {
                                       DisplayName = Convert.ToInt32(tv.Element("title").Value),
                                       ChannelURL = Convert.ToInt32(tv.Element("category").Value),
                                      ImageSource = Convert.ToInt32(tv.Element("url").Value),



                                   };
        }
        }

        public class TVNZ
        {
            public int DisplayName { get; set; }
            public int ChannelURL { get; set; }
            public int ImageSource { get; set; }



        }





    }

页: 1

<phone:PhoneApplicationPage 
    x:Class="tvGuide.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:task="clr-namespace:Microsoft.Phone.Tasks;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox Height="595" HorizontalAlignment="Left" Margin="6,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="444">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                            <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                            <StackPanel Width="370">
                                <TextBlock Text="{Binding DisplayName}" Foreground="#FFC8AB14" FontSize="28" />
                                <TextBlock Text="{Binding ChannelURL}" TextWrapping="Wrap" FontSize="24" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

如今,XML在问题中存档:

<tv date="20110708031152" generator-info-name="epgsnoop/0.84" generator-info-url="http://launchpad.net/epgsnoop">
- <channel id="maori-tv.freeviewnz.tv">
  <display-name>Maori TV</display-name> 
  <icon src="http://nzepg.org/logos/maori.jpg" /> 
  <url>http://www.freeviewnz.tv/</url> 
  </channel>
- <channel id="georgefm.freeviewnz.tv">
  <display-name>GeorgeFM</display-name> 
  </channel>
- <channel id="basefm.freeviewnz.tv">
  <display-name>BaseFM</display-name> 
  </channel>
- <channel id="rnz-concert.freeviewnz.tv">
  <display-name>RNZ Concert</display-name> 
  </channel>
- <channel id="stratos.freeviewnz.tv">
  <display-name>Stratos</display-name> 
  <icon src="http://nzepg.org/logos/stratos.jpg" /> 
  <url>http://www.stratostv.co.nz/</url> 
  </channel>
- <channel id="tv3.freeviewnz.tv">
  <display-name>TV 3</display-name> 
  <icon src="http://nzepg.org/logos/tv3.jpg" /> 
  <url>http://www.tv3.co.nz/</url> 
  </channel>
- <channel id="four.freeviewnz.tv">
  <display-name>Four</display-name> 
  <icon src="http://nzepg.org/logos/four.jpg" /> 
  </channel>
- <channel id="tvnz7.freeviewnz.tv">
  <display-name>TVNZ 7</display-name> 
  <icon src="http://nzepg.org/logos/tvnz7.jpg" /> 
  <url>http://www.freeviewnz.tv/</url> 
  </channel>
- <channel id="parliament.freeviewnz.tv">
  <display-name>Parliament</display-name> 
  <icon src="http://nzepg.org/logos/parliament.jpg" /> 
  <url>http://www.freeviewnz.tv/</url> 
  </channel>

任何帮助都将受到高度赞赏。

最佳回答

我试图这样做,但你法典中有一些问题。

  1. all those field must be string not int!
  2. all are optional to need to check against null
  3. attribute names were reading something different than the ones in xml!

这项工作:

var r = XDocument.Parse(e.Result);
listBox1.ItemsSource = from tv in r.Root.Descendants("channel")
                        let nameEl = tv.Element("display-name")
                        let urlEl = tv.Element("url")
                        let iconEl = tv.Element("icon")
                        select new TVNZ
                        {
                            DisplayName = nameEl == null ? null : nameEl.Value,
                            ChannelURL = urlEl == null ? null : urlEl.Value,
                            ImageSource = iconEl == null ? null : iconEl.Attribute("src").Value,
                        };

还将班级改为:

public class TVNZ
{
    public string DisplayName { get; set; }
    public string ChannelURL { get; set; }
    public string ImageSource { get; set; }
}

结论: 第一,在开始编码之前界定问题领域。


只有一个方面,即利用WebClient完全在ID thread上运行,你的申请没有反应,直到回复为止。 因此,它建议使用HttpWebRequest, 而后者可以单独进行。

我的博客 我为网络下载目的准备了一个求助班。 自由使用感。

问题回答

暂无回答




相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签