在列表框中绑定web xml属性

时间:2012-01-11 05:52:03

标签: windows-phone-7

我绑定了web xml,但它只读取了第一条记录

XML

<?xml version="1.0"?>
<content>
  <content_row id="1" day="1" title="test" from="01:10" first_name="jitendra" last_name="shakyawar" about_keynote="test" image="1326091608.jpg" innhold="1" about_speaker="test" desc="" flattr_url=""/>
  <content_row id="4" day="1" title="test 4" from="04:20" first_name="" last_name="" about_keynote="" image="" innhold="2" about_speaker="" desc="Test 4" flattr_url=""/>
</content>

XAML:

<cc:TabControl HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,80,0,0">
                <cc:TabItem Name="tabDag1" Height="50" Width="80" Header="Dag 1"  Style="{StaticResource TabItemStyle1}" Foreground="Black" >
                <Grid x:Name="ContentGrid" Grid.Row="1" HorizontalAlignment="Center" Margin="5,0,0,0">
                    <ListBox  Name="listDag1" Width="440" Background="Black">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal"   Height="auto" HorizontalAlignment="Left" Margin="0,20,20,0">
                                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"  Text="{Binding From}" FontWeight="Bold" FontSize="28"/>
                                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding FirstName}"/>
                                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding LastName}"/>
                                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding AboutSpeaker}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </cc:TabItem>
    </cc:TabControl>

C#

XDocument xdoc = XDocument.Parse(e.Result);
var data = from query in xdoc.Descendants("content")
           select new ContentItems
           {
               FirstName = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("first_name").Value,
               LastName = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("last_name").Value,
               From = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("from").Value,
               AboutSpeaker = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("about_speaker").Value
           };

listDag1.ItemsSource = data;

1 个答案:

答案 0 :(得分:1)

正如评论中指出的那样,你误解了如何使用Linq2xml。您不应在查询中包含根元素。所以你的查询应该看起来像这样:

var data =
        from query in xdoc.Descendants("content_row")
        select new ContentItems
        {
            FirstName = query.Element("content_row").Attribute("first_name").Value,
            LastName = query.Element("content_row").Attribute("last_name").Value,
            From = query.Element("content_row").Attribute("from").Value,
            AboutSpeaker = query.Element("content_row").Attribute("about_speaker").Value
        };

当然,在缺少属性的情况下,您必须手动检查它。