My LINQ query returns only 1 result

My LINQ query returns only the first result (class). Here is the code I'm using:

        XDocument xmlDoc = XDocument.Load("Decks/Test.xml");
        List<Cards> tempdeck = (from deck in xmlDoc.Elements("Deck")
                                select new Cards
                                 {
                                     Name = deck.Element("Type").Value

                                 }).ToList<Cards>();

        foreach (var item in tempdeck)
        {
            ((MessageBroker)App.Current.Resources["MessageBroker"]).GameLog.Add(item.Name.ToString());

        }

This is what my XML file looks like:

<Deck>
  <Type>
    <Name>Class</Name>
  </Type>
  <Type>
    <Name>stsfs</Name>
  </Type>
  <Type>
    <Name>Class</Name>
  </Type>
  <Type>
      <Name>Class</Name>
    </Type>
</Deck>

I format it this way, because when I get it to work, I want to add several properties to the request, not just the name.

Thanks in advance!

+3
source share
2 answers

This is not clear from your example, but it looks like you have one item Deckwith multiple children Type. Your code takes the opposite, that is, several decks, each of which has one (or one interesting) type-child.

Try this instead:

( from type in xmlDoc.Element("Deck").Elements("Type")
  select new Cards { Name = type.Value }
).ToList()

"Cards" are created from each type of child of a single deck.

, Mark Cidade.

+3

Deck, Type. Type:

    List<Cards> tempdeck = (from deck in xmlDoc.Elements("Deck")
                            from type in deck.Elements("Type")
                            select new Cards
                             {
                                 Name = type.Value

                             }).ToList<Cards>();
+1

Source: https://habr.com/ru/post/1793334/


All Articles