1

嗨,我正在使用下面给出的 xml 文件,我尝试了很多代码来从 xml 文件中获取多个列表,但我无法获得。

<?xml version="1.0" encoding="utf-8" ?>
  <root>
    <Categories>
      <Category name="Photos">
        <Articles>
          <article  title="Sherawat's">
            <Description>
               Hottie Sherawat 
            </Description>
            <FullContent>
             <style> img {padding:2px;} </style><p> <img alt=" Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_mallika-sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p> <p>Sherawat is all set to begin shooting for&nbsp;<em>Dirty Politics</em>&nbsp;where she plays Bhanwari Devi, a nurse whose murder hit the headlines last year. Meanwhile, she gets talking on&nbsp;Hollywood where she has not really had any meaty roles.</p>
            </FullContent>
            <thumb_image>
              <image  url="http://sss.com/Photo1.jpeg"/>"/>
            </thumb_image>
        </article>
        <article articleid="2684" title="Steals the Mai Show">
        <Description>
           <p> Actor Hrithik Roshan has always nursed a dream of working with acclaimed Director Kapur.</p>
        </Description>
       <FullContent>
        <div id="container" class="cf">
           <link rel="stylesheet" href="http://sss.com/imageslider/app/css/demo.css" type="text/css" media="screen" /> 
         <link rel="stylesheet" href="http://sss.com/imageslider/app/css/flexslider.css" type="text/css" media="screen" /> 
                 <div id="main" role="main"> <section class="slider"> <div class="flexslider"> <ul class="slides">
                   <li>Sonam Kapoor<img src="http://sss.com//website/var/tmp/thumb_5814_1_01feb2013__appfeed.jpeg" alt="Kapoor"/></li>
          </ul>
          </li>
         </div>
       </FullContent>
       <thumb_image>
            <image  url="http://Photo2.jpeg"/>"/>
       </thumb_image>
     </article>
 </Articles>
 </Category>
 <Category name="Videos">
<Articles>
   <article articleid="415" title=" Dirty Politics">
      <Description>
         Sherawat speaks about the men whom she’s over the moon about
      </Description>
      <FullContent>
        <style> img {padding:2px;} </style><p> <img alt="Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://sss.com/bolly/feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p>
      </FullContent>
      <thumb_image>
        <image  url="http://Video1.jpeg"/>"/>
      </thumb_image>
    </articles>
    <article articleid="68" title="Digital!">
     <Description>
         <p> Touch, tap, flip, slide! You don&#39;t, you experience it.</p>
      </Description>
      <FullContent>
        <p> Touch, tap, flip, slide! You don&#39;you experience it.</p> <br/><br/><br/> <br/><br/>
      </FullContent>
      <thumb_image>
        <image  url="http://Video2.jpeg"/>"/>
      </thumb_image>
    </article>
 </Article>
</Category>
 <Category name="Bolly">
   <Articles>
     <article articleid="415" title="upcoming movie">
       <Description>
           the men whom she’s over the moon about
       </Description>
       <FullContent>
        <style> img {padding:2px;} </style><p> <img alt="Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://sss.com/bolly/feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p>
      </FullContent>
       <thumb_image>
         <image url="http://sss.com/website/var/tmp/thumb_5854_90_mallika-sherawat_thumb_bolly__forfeed.jpeg"/>
       </thumb_image>
       </articles>
       <article articleid="436" title="Surprise Package">
         <Description>
           There was more than just good music at the trio's recent performance
         </Description>
         <FullContent>
           <style> img {padding:2px;} </style><p> <img alt="Akcent" pimcore_disable_thumbnail="true" pimcore_id="6110" pimcore_type="asset" src="http://dev2.mercuryminds.com/global/feb2013/surprise-package-at-akcent-concert/18_akcent_global.jpg" style="width: 370px; height: 500px; float: left;" /></p>
         </FullContent>
         <thumb_image>
           <image url="http://sss.com/website/var/tmp/thumb_6109_18_akcent_thumb__forfeed.jpeg"/>
         </thumb_image>
       </article>
    </Article>
 </Category>
 </Categories>
</root>

我的 MainPageXaml.cs 代码

 public MainPage()
    {
        InitializeComponent();

        WebClient downloader = new WebClient();
        Uri uri = new Uri("http://sss.com/webservices/new_feed_articls.xml", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(NotchsDownloaded);
        downloader.DownloadStringAsync(uri);
    }

  void NotchsDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the XML-file!");
        }
         else
         {
             ParseXMLFile(e.Result);
         }
    }

    void ParseXMLFile(string  dataInXmlFile)
    {
        try
        {
          //Parsing XML File


            XDocument xmlDoc = XDocument.Parse(dataInXmlFile);

            var query = from l in xmlDoc.Descendants("Category")
                        select new Data
                        {
                            name = (string)l.Attribute("name").Value,
                            Titles = l.Element("Articles").Elements("article")
                                     .Select(s => s.Attribute("title").Value)
                                     .ToList(),


                            Image = l.Element("Articles").Elements("article")
                                     .Elements("thumb_image").Elements("image")
                                     .Select(x => x.Attribute("url").Value).ToList()
                        };
                    foreach (var result in query)
                    {
                      Console.WriteLine(result.name);
                         foreach (var detail in result.Titles)
                         {
                           Console.WriteLine(detail);
                         }
                     }
                     List.ItemsSource = query.ToList();

        }
        catch(Exception e)
        {
        }

    }

我的班级档案

public class Data
{

    [XmlAttribute("name")]
        public string name { get; set; }

    [XmlAttribute("title")]
    public List<string> Titles { get; set; }

    [XmlAttribute("url")]
    public List<string> Image { get; set; }

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

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


}

[XmlRoot("root")]
public class DataArray
{
    [XmlArray("Categories")]
    [XmlArrayItem("Category")]
    [XmlArrayItem("Articles")]
     [XmlArrayItem("article")]
    public ObservableCollection<Data> Collection { get; set; }
}

我的 XAML 文件

<ListBox x:Name="List" ItemsSource="{Binding}"
           Margin="0,-0.25,2,-6" Grid.Row="3" HorizontalAlignment="Right" Width="478" d:LayoutOverrides="VerticalMargin" Grid.RowSpan="2">
        <ListBox.ItemTemplate>
            <DataTemplate>
                  <StackPanel Margin="0,0,0,0"  Orientation="Vertical" Grid.ColumnSpan="2" Grid.RowSpan="3" x:Name="ControlsPanel"
                        Grid.Column="0"
                        Height="160"
                        VerticalAlignment="Top">
                        <StackPanel Background="#eb2427" Orientation="Vertical">
                            <TextBlock Grid.Row="1"  FontFamily="Calibri" FontSize="34" FontWeight="Bold"  FontStyle="Normal" Margin="10,0,0,0"
                                Text="{Binding name}" 
                                   />               
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Grid.Row="1" FontFamily="Calibri" FontSize="32" Foreground="#a7a9ac" Width="Auto"
                                Text="{Binding Titles}" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
                        </StackPanel>
                        <StackPanel>
                                <Image Source="{Binding Image}" Width="Auto"></Image>
                        </StackPanel>
                    </StackPanel>       
            </DataTemplate> 
        </ListBox.ItemTemplate>
    </ListBox>

我尝试了很多代码,但我得到了相同的结果。输出没有变化。我在我的输出屏幕短裤下面给出

在此处输入图像描述

我想要像这样的输出

在此处输入图像描述

嗨,我尝试了很多东西,但很长时间都无法解决这个问题。所以现在我告诉你我的期望。请任何人考虑我的问题并给我任何代码来解决这个问题。

4

2 回答 2

1

您必须ListBox在您的ListBox.ItemTemplate, 而不是该部分中创建另一个:

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Grid.Row="1" FontFamily="Calibri" FontSize="32" Foreground="#a7a9ac" Width="Auto"
                            Text="{Binding Titles}" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
                    </StackPanel>
                    <StackPanel>
                            <Image Source="{Binding Image}" Width="Auto"></Image>
                    </StackPanel>
于 2013-03-20T08:47:04.370 回答
1

首先,将 Image 属性重命名为 Images,因为它是一个图像列表,而不是单个图像。

然后,在 XAML 代码中,将该图像列表的显示方式从显示一个图像调整为多个图像。这与显示集合的方式相同 - 通过使用 ListBox(或其他适当的 ItemsControl)。将 XAML 更改为:

<ListBox ItemsSource="{Binding Image}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Width="Auto"></Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这将显示一个图像列表,而不是显示一个包含一个图像的 StackPanel。但它将垂直而不是水平显示它们。查看以下线程以获取更多信息:如何让水平列表框在 WP7 中水平滚动?.

于 2013-03-20T14:30:15.833 回答