1

したがって、基本的には以下のコードを実行しています。取得するのはすべて1つのアイテムです。XML 応答を確認しましたが、取得した項目よりも多くの項目が含まれています。なぜこれが起こっているのか分かりません。

スクリーンショットアプリprntscreenはこちら

ここでは、XML 応答をセットアップする方法を示します。HotUKDeals API xml

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        getResult();
    }
    private async void getResult()
    {
        progressBar1.IsIndeterminate = true;
        progressBar1.Visibility = Visibility.Visible;

        string strRequestUri = "http://api.hotukdeals.com/rest_api/v2/?key=MY_API_KEY_GOES_HERE&deals&results_per_page=10&page=1";
        string strResult = string.Empty;

        try
        {
            HttpClient httpClient = new HttpClient();
            try
            {
                strResult = await httpClient.GetStringAsync(strRequestUri);

                // GetByteArrayAsync
                //byte[] data = await httpClient.GetByteArrayAsync(strRequestUri);
                //strResult = data.Length.ToString();

                progressBar1.IsIndeterminate = true;
                progressBar1.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
            }
        }
        catch (Exception ex)
        {
            strResult = ex.Message;
        }
        finally
        {
            // Show the result.
            this.Dispatcher.BeginInvoke(delegate()
            {


                //tbResult.Text = strResult;

                XElement wc = XElement.Parse(strResult);

var postList = 
                    from deal in wc.Descendants("deals")
                    select new Deals

{
    dealimage = deal.Element("api_item").Element
("deal_image").Value,//  tweet.Element("user").Element("profile_image_url").Value,
    dealimageHR = deal.Element("api_item").Element
("deal_image_highres").Value,
    Title = deal.Element("api_item").Element
("title").Value, //tweet.Element("user").Element("screen_name").Value,
    MobLink = deal.Element("api_item").Element
("mobile_deal_link").Value,
    MadeHot = deal.Element("api_item").Element
("hot_time").Value,
    dealtemp = deal.Element("api_item").Element
("temperature").Value,
    PostBy = deal.Element("api_item").Element
("poster_name").Value,
    ShopName = deal.Element("api_item").Element
("merchant").Element("name").Value,
    submited = Convert.ToString(deal.Element
("api_item").Element("submit_time").Value).Substring(0, 20)
                                           };
                dealsListbox.ItemsSource = postList.ToList();
            });               
        }
    }

    private void refreshBTN_Click(object sender, RoutedEventArgs e)
    {
        getResult();
    }
}

}

ここに MainPage.xaml があります

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector Name="dealsListbox" Width="480" Height="719" 
VerticalAlignment="Bottom" Padding="0,0,0,95" Margin="-12,0" >
            </phone:LongListSelector.Background>
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="5,5,5,0" Background="#FF636363">
                        <StackPanel Width="125">
                            <Image Source="{Binding dealimageHR}" 
                                   Height="115" Width="120" 
                                   VerticalAlignment="Center" Margin="5,5,0,0" 
                                   Stretch="UniformToFill" />
                            <TextBlock Text="{Binding dealtemp}" 
                                       Foreground="OrangeRed" 
                                       FontSize="27"  
                                       FontWeight="Bold" 
                                       HorizontalAlignment="Left" Margin="5,0,5,5" />
                        </StackPanel>
                        <StackPanel Width="340" Margin="8,5,0,0">
                            <TextBlock Text="{Binding Title}" 
                                       Foreground="Orange" 
                                       TextWrapping="Wrap" 
                                       FontSize="22" 
                                       FontWeight="SemiBold" HorizontalAlignment="Left" VerticalAlignment="Top" 
                                       Height="98" Padding="0,0,1,0" />
                            <!--<TextBlock Text="{Binding Description}" Foreground="DarkGray" FontSize="18" TextWrapping="Wrap" Margin="0,5,0,10" />-->
                            <!--<TextBlock Text="{Binding MobLink}" Foreground="Azure" FontSize="18" TextWrapping="Wrap" Margin="0,5,0,10" />-->

                            <TextBlock FontSize="16" FontWeight="Light"  Foreground="Red" >
                                <Run Text="Posted"/>
                                <Run Text="{Binding submited}" />
                                <Run Text="by:"/>
                                <Run Text="{Binding PostBy}"    />
                            </TextBlock>
                            <TextBlock  Foreground="GreenYellow">
                                <Run Text="Made Hot" />
                                <Run Text="{Binding MadeHot}" FontWeight="Bold"/>
                            </TextBlock>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
             </phone:LongListSelector.ItemTemplate>
         </phone:LongListSelector>

そして DealClass.cs

class Deals
{
    public string Title { get; set; }
    public string dealimage { get; set; }
    public string Description { get; set; }
    public string submited { get; set; }
    public string dealtemp { get; set; }
    public string MobLink { get; set; }

    public string dealimageHR { get; set; }

    public string MadeHot { get; set; }

    public string PostBy { get; set; }
}
4

1 に答える 1

1

コンソールアプリでコードを再作成することで機能します。次のコードは機能します。3 つの api_item を持つように xml を設定すると、3 つすべてが返されます。「in」句を拡張して、deal 要素から api_item 要素を選択していることに注意してください。

    XElement wc = XElement.Parse(System.IO.File.ReadAllText("XMLFile1.xml"));

    var postList = from deal in wc.Descendants("deals").Elements("api_item")
                   select new Deals
                   {
                       dealimage = deal.Element("deal_image").Value,
                       dealimageHR = deal.Element("deal_image_highres").Value,
                       Title = deal.Element("title").Value, 
                       MobLink = deal.Element("mobile_deal_link").Value,
                       MadeHot = deal.Element("hot_time").Value,
                       dealtemp = deal.Element("temperature").Value,
                       PostBy = deal.Element("poster_name").Value,
                       submited = Convert.ToString(deal.Element("submit_time").Value).Substring(0, 20)
                   };

}
于 2013-11-14T18:03:46.123 に答える