UWP - Plik XML - Data Binding

0

Czesc mam plik xml w ktorym mam kilka ksiazek. Probuje zbindowac je do text block ale zadne dane nie sa wyswietlone. Wyswietlaja sie 4 boxy wiec wiem ze plik jest poprawnie widziany bo sa w nim 4 ksiazki. Jakies wskazowki ? Dzieki
XML

<?xml version="1.0" encoding="UTF-8"?>

<root>
  <books>
    <element>
      <category>Thriller</category>
      <description>In The Green Line, Abby Donovan's decade-long dream of partnership is now just months from fruition. But after a late-night train mishap drops her into a world of drugs, gangbangers, murder, and corruption, everything changes</description>
      <id>1</id>
      <image>images/greenLine.jpg</image>
      <price>10.50</price>
      <title>The Green Line</title>
    </element>
    <element>
      <category>Comedy</category>
      <description>In The Green Line, Abby Donovan's decade-long dream of partnership is now just months from fruition. But after a late-night train mishap drops her into a world of drugs, gangbangers, murder, and corruption, everything changes</description>
      <id>1</id>
      <image>images/greenLine.jpg</image>
      <price>10.50</price>
      <title>AAA</title>
    </element>
    <element>
      <category>Thriller</category>
      <description>In The Green Line, Abby Donovan's decade-long dream of partnership is now just months from fruition. But after a late-night train mishap drops her into a world of drugs, gangbangers, murder, and corruption, everything changes</description>
      <id>1</id>
      <image>images/greenLine.jpg</image>
      <price>10.50</price>
      <title>BBBB</title>
    </element>
    <element>
      <category>Thriller</category>
      <description>In The Green Line, Abby Donovan's decade-long dream of partnership is now just months from fruition. But after a late-night train mishap drops her into a world of drugs, gangbangers, murder, and corruption, everything changes</description>
      <id>1</id>
      <image>images/greenLine.jpg</image>
      <price>10.50</price>
      <title>The Green Line</title>
    </element>
  </books>
</root>

XAML:

   <GridView x:Name="DataGrid1">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid  Background="AliceBlue" Width="300" Height="200">
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}"></TextBlock>
                            <TextBlock Text="{Binding Category}"></TextBlock>
                          
                        </StackPanel>
                    </Grid>
                   </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

C#

 string XMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "booksData/data.xml");
            XDocument loadedD = XDocument.Load(XMLPath);

            var newData = from query in loadedD.Descendants("element")
                          select new Book
                          {
                              Title = (string)query.Attribute("title"),
                              Category = (string)query.Attribute("category")

                          };
            DataGrid1.ItemsSource = newData;
1

Metoda Attribute() zwraca wartość atrybutu, więc zadziałałaby, gdybyś miał strukturę postaci <element title="aaa"></element>. Ty potrzebujesz dla każdej książki dostać się do elementów w środku niej i dopiero do wartości, czyli coś w stylu: query.Descendants("title").First().Value:

var newData = from query in loadedD.Descendants("element")
              select new Book
              {
                  Title = (string)query.Descendants("title").First().Value,
                  Category = (string)query.Descendants("category").First().Value
              };
0
Ktos napisał(a):

Metoda Attribute() zwraca wartość atrybutu, więc zadziałałaby, gdybyś miał strukturę postaci <element title="aaa"></element>. Ty potrzebujesz dla każdej książki dostać się do elementów w środku niej i dopiero do wartości, czyli coś w stylu: query.Descendants("title").First().Value:

var newData = from query in loadedD.Descendants("element")
              select new Book
              {
                  Title = (string)query.Descendants("title").First().Value,
                  Category = (string)query.Descendants("category").First().Value
              };

Dziekuje :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1