Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从同名的XML WP7中选择多个条目_C#_Xml_Silverlight_Windows Phone 7_Linq To Xml - Fatal编程技术网

C# 从同名的XML WP7中选择多个条目

C# 从同名的XML WP7中选择多个条目,c#,xml,silverlight,windows-phone-7,linq-to-xml,C#,Xml,Silverlight,Windows Phone 7,Linq To Xml,我已经尝试了几个小时,并在网上搜索了一些示例,了解如何提取具有相同名称但属性不同的多个元素,以及如何在我的wp7应用程序中将这些元素绑定到我的XAML 对我来说,最简单的解释方式就是向你展示 这是我到目前为止得到的 public class Match { public string HomeTeam { get; set; } public string AwayTeam { get; set; } public strin

我已经尝试了几个小时,并在网上搜索了一些示例,了解如何提取具有相同名称但属性不同的多个元素,以及如何在我的wp7应用程序中将这些元素绑定到我的XAML

对我来说,最简单的解释方式就是向你展示

这是我到目前为止得到的

       public class Match
    {
        public string HomeTeam { get; set; }
        public string AwayTeam { get; set; }
        public string HomeScore { get; set; }
        public string AwayScore { get; set; }
        public string GoalsPlayer { get; set; }
        public string goal { get; set; }
        public string GoalsTime { get; set; }
        public string DismissalsPlayer { get; set; }
        public string DismissalsTime { get; set; }
        public string BookingPlayer { get; set; }
        public string BookingTime { get; set; }
        public string GameTime { get; set; }
    }

 void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        XElement element = XElement.Parse(e.Result);
        try
        {
            listBox2.ItemsSource = from item in element.Descendants("competition")
                                   from match in item.Elements("match").Where(arg => arg.Attribute("awayTeam").Value == team | arg.Attribute("homeTeam").Value == team)
                                   select new Match
                                   {
                                       HomeTeam = (string)match.Attribute("homeTeam"),
                                       AwayTeam = (string)match.Attribute("awayTeam"),
                                       HomeScore = (string)match.Attribute("homeTeamScore"),
                                       AwayScore = (string)match.Attribute("awayTeamScore"),
                                       GoalsPlayer = (string)match.Attribute("playerName"),
                                       GoalsTime = (string)match.Attribute("time"),
                                   };
        }
        catch (Exception ex)
        {
             Debug.WriteLine(ex.StackTrace);
        }
这是我的XAML

<ListBox Name="listBox2" Grid.Row="0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Name="teams" Orientation="Vertical">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/>
                                    <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" />
                                    <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/>
                                    <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" />
                                </StackPanel>
                                <StackPanel Name="scores" Orientation="Horizontal">
                                    <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding GoalsPlayer}" TextWrapping="Wrap" FontSize="20" />
                                    <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding GoalsTime}" TextWrapping="Wrap" FontSize="20" />
                                </StackPanel>
                                <StackPanel Name="dismissals">
                                    <TextBlock Foreground="White" Text="{Binding DismissalsPlayer}" TextWrapping="Wrap" FontSize="20"/>
                                    <TextBlock Foreground="White" Text="{Binding DismissalsTime}" TextWrapping="Wrap" FontSize="20"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

这是我的XML

<match homeTeam="Arsenal" awayTeam="Liverpool" homeTeamScore="0" awayTeamScore="2">
<goal time="77:13" playerName="Aaron Ramsey" />
<goal time="89:57" playerName="Luis Suarez"/>
<dismissal time="69:59" playerName="Emmanuel Frimpong"/>
</match>

我知道我的页面上只有一个目标条目,这将是XML中的第一个目标元素

我如何才能做到这一点,并实施每一项计划

<goal ...>
<goal ...>

在我目前只显示第一个条目的数据模板中,另一个潜在的问题是我不能保证会有多少个目标,所以我真的不确定如何完全实现它

谢谢


约翰

是否应该playerName甚至解决你的
匹配问题
?因为这就是在XML查询中解析它的地方

不管怎样,您在这里寻找的是一个子对象,它可以保存在
Match
对象的列表属性中:

listBox2.ItemsSource = from item in element.Descendants("competition")
                       from match in item.Elements("match")
                           .Where(arg => arg.Attribute("awayTeam").Value == team || 
                                         arg.Attribute("homeTeam").Value == team)
                       select new Match
                       {
                           HomeTeam = (string)match.Attribute("homeTeam"),
                           AwayTeam = (string)match.Attribute("awayTeam"),
                           HomeScore = (string)match.Attribute("homeTeamScore"),
                           AwayScore = (string)match.Attribute("awayTeamScore"),
                           Goals = match.Elements("goals").Select(ev => new MatchEvent
                           {
                               Player = (string)ev.Attribute("playerName"),
                               Time = (string)ev.Attribute("time")
                           }).ToList(),
                           Dismissals = match.Elements("dismissals").Select(ev => new MatchEvent
                           {
                               Player = (string)ev.Attribute("playerName"),
                               Time = (string)ev.Attribute("time")
                           }).ToList(),
                       };
以及更新的XAML:

<ListBox Name="listBox2" Grid.Row="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
             <StackPanel Name="teams" Orientation="Vertical">
             <StackPanel Orientation="Horizontal">
                  <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/>
                  <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" />
                  <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/>
                  <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" />
              </StackPanel>
              <ItemsControl ItemsSource="{Binding Goals}">
                  <ItemsControl.ItemTemplate>
                      <DataTemplate>
                          <StackPanel Name="scores" Orientation="Horizontal">
                              <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" />
                              <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" />
                          </StackPanel>
                      </DataTemplate>
                  </ItemsControl.ItemTemplate>
              </ItemsControl>

              <ItemsControl ItemsSource="{Binding Dismissals}">
                  <ItemsControl.ItemTemplate>
                      <DataTemplate>
                          <StackPanel Name="scores" Orientation="Horizontal">
                              <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" />
                              <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" />
                          </StackPanel>
                      </DataTemplate>
                  </ItemsControl.ItemTemplate>
              </ItemsControl>
          </StackPanel>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>


您能否详细说明您实际想要实现的目标?我不清楚你的最后两段。对不起,当然,我的数据绑定只显示我页面上的第一个条目,我相信这是因为我只处理一个条目,我不确定如何显示我页面上的每个条目。目前页面只显示Aaron Ramsey的条目,而不显示Luis Suarez的条目。请解释一下这是如何工作的,我假设MatchEvents将是一个新类?如何将列表绑定到数据模板中的单个文本块条目?是的,玩家名称应该是,因为我需要得分者的姓名,
MatchEvent
是一个单独的类,概括了进球和解围。您无法将列表绑定到文本框,您必须将子
ItemsControl
添加到
itemstemplate
中,并将其
ItemsSource
绑定到
MatchEvents
属性。在查看您的问题后,我将目标/解雇拆分为单独的对象/列表。我还添加了更新的XAML以更好地解释嵌套项控件。@John-谢谢,我在回答中也修复了它:)