2012-04-16 9 views
0

私は今日映画を見るためにムービーのムービーの日付を表示しようとしています。私は一日中、別のスレッドを読んでいます。購入することはできません。HttpWebRequest xmlで何も表示されない構文解析

基本的に私はwebClientでコードを作業していましたが、UIが応答しやすいようにしましたので、httpWebRequestを使用してUIスレッドからxml解析を引き継ぐことにしました。

public partial class MainPage : PhoneApplicationPage { 



public MainPage() { 
    InitializeComponent(); 
} 

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
    DoHttpWebRequest(); 
} 


private void DoHttpWebRequest() { 
    string url = "http://www.cinamon.ee/rss/schedule/1001.xml"; 
    var request = HttpWebRequest.Create(url); 
    var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
} 

private void ResponseCallback(IAsyncResult result) { 
    var request = (HttpWebRequest)result.AsyncState; 
    var response = request.EndGetResponse(result); 

    using (var stream = response.GetResponseStream()) { 

    XDocument scheduleXml = XDocument.Load(stream); 
    var todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
         where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
         DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
         select new Movie() { 
         MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
         MovieName = (string)query.Element("title"), 
         MovieId = (string)query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
         }; 

    // Removing duplicate movies from list. 
    List<Movie> todayList = todayMovies.ToList(); 
    IEnumerable<Movie> noDuplicates3 = todayList.Distinct(new MovieComparer()); 

    // Adding to the UI 
    Dispatcher.BeginInvoke(() => { 
     todayBox.ItemsSource = noDuplicates.ToList(); 
    }); 
    } 

} 
} 

誰もがこのコードを見て何が間違っているのようなアイデアを持っていますか?

ありがとうございました EDIT。これは私のソリューションに基づくリンクです - http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/594e1422-3b69-4cd2-a09b-fb500d5eb1d8

EDIT2私のMainpage.xaml

<StackPanel x:Name="TodayPanel" Grid.Row="1" Margin="10,5,10,10" Orientation="Horizontal" Height="580" Background="#90000000" > 
     <ListBox x:Name="todayBox"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
      <HyperlinkButton x:Name="hyperLinkButton" Style="{StaticResource HyperlinkButtonStyle1}" CommandParameter="{Binding MovieId}" Tap="hyperLinkButton_Tap"> 
       <HyperlinkButton.Content> 
       <StackPanel Margin="10" Grid.Row="1" Orientation="Horizontal"> 
        <Image Source="{Binding MoviePicture}" /> 
        <StackPanel Margin="10" Grid.Row="1" Orientation="Vertical"> 
        <TextBlock TextWrapping="Wrap" Margin="10, 5, 10, 5" Width="200" FontFamily="Trebuchet MS" Foreground="Orange" VerticalAlignment="Top"> 
             <Run Text="{Binding MovieName}"/> 
             <LineBreak></LineBreak> 
        </TextBlock> 
        <TextBlock TextWrapping="Wrap" Width="200" FontFamily="Trebuchet MS" Foreground="White" VerticalAlignment="Bottom"> 
             <Run Text="Järgmine seanss: "/> 
             <LineBreak></LineBreak> 
             <Run Text="{Binding MovieSoonest}"/> 
        </TextBlock> 
        </StackPanel> 
       </StackPanel> 
       </HyperlinkButton.Content> 
      </HyperlinkButton> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 

私の編集したコードの背後にあります。

private void DoHttpWebRequest() { 
    string url = "http://www.cinamon.ee/rss/schedule/1001.xml"; 
    var request = HttpWebRequest.Create(url); 
    var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
} 

private void ResponseCallback(IAsyncResult result) { 
    var request = (HttpWebRequest)result.AsyncState; 
    var response = request.EndGetResponse(result); 

    // Adding to the UI 
    Dispatcher.BeginInvoke(() => { 
    IEnumerable<Movie> todayMovies; 
    using (var stream = response.GetResponseStream()) { 

     XDocument scheduleXml = XDocument.Load(stream); 
     todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
        where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
        DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
        select new Movie() { 
         MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
         MovieName = (string)query.Element("title"), 
         MovieId = (string)query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
        }; 
    } 

     var todayList = todayMovies.ToList(); 
     //IEnumerable<Movie> noDuplicates = movieList.Distinct(new MovieComparer()); 

     todayBox.ItemsSource = todayList.ToList(); 



    }); 

答えて

1

私はあなたのコードを試して、UnauthorizedAccessExceptionを取得しました。 Dispactcher.Begininvokeデリゲートのスコープを変更することにより、それは次のように動作します。

private void ResponseCallback(IAsyncResult result){ 
var request = (HttpWebRequest) result.AsyncState; 
var response = request.EndGetResponse(result); 
// Adding to the UI 
Dispatcher.BeginInvoke(() => 
{ 
IEnumerable<Movie> todayMovies; 
using (var stream = response.GetResponseStream()) 
{ 

    XDocument scheduleXml = XDocument.Load(stream); 
    todayMovies = 
     from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
     where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
       DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 
       0 
     select new Movie() 
        { 
         MoviePicture = 
          new BitmapImage(
          new Uri((string) query.Element("images").Element("imageType2").Value, 
            UriKind.RelativeOrAbsolute)), 
         MovieName = (string) query.Element("title"), 
         MovieId = (string) query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
        }; 

} 
// Removing duplicate movies from list. 
var todayList = todayMovies.ToList(); 
    //IEnumerable<Movie> noDuplicates3 = todayList.Distinct(new MovieComparer()); 


            todayBox.ItemsSource = todayList.ToList(); 
           }); 

}

しかし、あなたはそれを容易にするためには(あなたがNugetでそれを見つけること)RestSharpライブラリを使用することができます。 xaml.csのDataTemplateの:から

 <StackPanel x:Name="TodayPanel" Grid.Row="1" Margin="10,5,0,10" Orientation="Horizontal" Height="580" Background="#90000000" > 
     <ListBox x:Name="todayBox" Width="468"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="10" Orientation="Horizontal"> 
           <Image Source="{Binding MoviePicture, FallbackValue=http://www.cinamon.ee/visinternetticketing/images/movies/NowShowingComingSoon/HungerGames.jpg}" /> 
           <StackPanel Margin="10" Grid.Row="1" Orientation="Vertical"> 
            <TextBlock TextWrapping="Wrap" Margin="10, 5, 10, 5" FontFamily="Trebuchet MS" Foreground="Orange" VerticalAlignment="Top" Text="{Binding MovieName}"/> 
            <TextBlock TextWrapping="Wrap" FontFamily="Trebuchet MS" Foreground="White" VerticalAlignment="Bottom" Text="{Binding MovieSoonest}"/> 
           </StackPanel> 
           <HyperlinkButton x:Name="hyperLinkButton" CommandParameter="{Binding MovieId}" /> 
          </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 

RECALL 変更MovePictureプロパティ

EDITED ...

public void RestSample(){ 
var client = new RestClient 
{ 
    BaseUrl = "http://www.cinamon.ee/" 
}; 

var request = new RestRequest 
{ 
    Resource = "rss/schedule/1001.xml" 
}; 

client.ExecuteAsync(request, (a) => 
{ 
    if (a.StatusCode == HttpStatusCode.OK) 
    { 
     var scheduleXml = XDocument.Parse(a.Content); 

     var todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
          where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
          DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
          select new Movie() 
          { 
           MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
           MovieName = (string)query.Element("title"), 
           MovieId = (string)query.Element("movieId"), 
           MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
          }; 

     // Removing duplicate movies from list. 
     List<Movie> todayList = todayMovies.ToList(); 
     //IEnumerable<Movie> noDuplicates = todayList.Distinct(new MovieComparer()); 

     // Adding to the UI 
     Dispatcher.BeginInvoke(() => 
     { 
      todayBox.ItemsSource = todayList.ToList(); 
     }); 
    } 
    else 
    { 
     //error 
    } 
}); 

}

それを試してみて、私たちに知らせて次のコードをチェックしてくださいBitmapImageを文字列に設定します。

That's my result using DataTemplate above

+0

私はあなたが最初の投稿解決策を試してみましたが、それはまだ仕事を文句を言いません。エラーも何もない。ページは空です。 RestSharpを試したことがありません。なぜなら、そのライブラリで利用可能なチュートリアルの量が少ないため、ローディングバーを実装することができないのではないかと心配しています。私は後でそれを試してみるつもりです、おそらくそれは動作します – JoonasL

+0

何も言わないとき、todayBoxに何も表示されませんか?そのDataTemplateを作成しましたか?私はそれを試して、それは私のために働く。私に教えてください。 –

+0

mainpage.xamlと.csで元の投稿を編集しました。 webClient – JoonasL