0

I want to read my xml and then in c# code generate textblock to add all datas in their textlock, make something like a table or list.

My xml is:

    <?xml version="1.0"?>
    <players>
      <player>
        <id>10101</id>
        <name>Ricardo Ferreira Rodrigues</name>
        <shirtnumber>1</shirtnumber>
        <position>Guarda Redes</position>
     </player>
     <player>
        <id>10102</id>
        <name>Manuel Lopes</name>
        <shirtnumber>2</shirtnumber>
        <position>fixo</position>
     </player>
    </players>

And my code is:

    private async void LoadXml()
    {
        try
        {
            StorageFolder storageFolder = Package.Current.InstalledLocation;
            StorageFile storageFile = await storageFolder.GetFileAsync("players2.xml");

            string xml = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8);

            var doc = XDocument.Parse(xml);
            var rootNode = doc.Root;
            foreach (var child in rootNode.Descendants("player"))
            {
                //I try this
                //TextBlock txtnome = new TextBlock();
                //TextBlock txtshirtnumber = new TextBlock();
                //TextBlock txtposition = new TextBlock();
                txtnome.Text = (string)doc.Root.Element("name");
                txtshirtnumber.Text = (string)doc.Root.Element("shirtnumber");
                txtposition.Text = (string)doc.Root.Element("position");
            }

        }

but this textblock i make it and i want the code to generate then because I will need to add lot of players.

My Erro is "values cannot be null".

Someone can help me?

4

1 回答 1

2

尝试这个。你应该使用ItemsControl. 您将为它提供模板,并且它将为您的可用数据重复。

XAML

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="32"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">

            <Border BorderBrush="White" BorderThickness="1">
                <TextBlock Text="ID" FontSize="25" Width="100"/>
            </Border>

            <Border BorderBrush="White" BorderThickness="1">
                <TextBlock Text="Name" FontSize="25"  Width="300"/>
            </Border>

            <Border BorderBrush="White" BorderThickness="1">
                <TextBlock Text="Shirt Number" FontSize="25"  Width="180"/>
            </Border>

            <Border BorderBrush="White" BorderThickness="1">
                <TextBlock Text="Position" FontSize="25"  Width="180"/>
            </Border>
        </StackPanel>

        <ItemsControl x:Name="ic" Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Left">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                            <Border BorderBrush="White" BorderThickness="1">
                                <TextBlock Text="{Binding ID}" FontSize="25" Width="100"/>
                            </Border>
                            <Border BorderBrush="White" BorderThickness="1">
                                <TextBlock Text="{Binding Name}" FontSize="25" Width="300"/>
                            </Border>
                            <Border BorderBrush="White" BorderThickness="1">
                                <TextBlock Text="{Binding ShirtNumber}" FontSize="25" Width="180"/>
                            </Border>
                            <Border BorderBrush="White" BorderThickness="1">
                                <TextBlock Text="{Binding Position}" FontSize="25" Width="180"/>
                            </Border>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Grid>

C#

private async Task LoadXml()
{
    StorageFolder storageFolder = Package.Current.InstalledLocation;
    StorageFile storageFile = await storageFolder.GetFileAsync("players2.xml");

    string xml = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8);

    var doc = XDocument.Parse(xml);
    var rootNode = doc.Root;
    foreach (var child in rootNode.Descendants("player"))
    {
        var objPlayer = new PlayerModel
        {
            ID = child.Element("id").Value,
            Name = child.Element("name").Value,
            ShirtNumber = child.Element("shirtnumber").Value,
            Position = child.Element("position").Value
        };

        Items.Add(objPlayer);
    }

    ic.ItemsSource = Items;
}

玩家模型.cs

public class PlayerModel
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string ShirtNumber { get; set; }
    public string Position { get; set; }
}
于 2013-09-05T14:12:35.577 回答