My LINQ Query is only returning the first result (Class). Here is the code that I am 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 am formatting it this way because when I get it to work I want to add multiple properties to the query – not just name.
Thanks in advance!
It isn’t clear from your sample, but it appears you have one
Deckelement with multipleTypechild elements. Your code is assuming the opposite, i.e. multiple decks, each with one (or one interesting) type-child.Try this instead:
A “cards” is produced from each type-child of the only deck.
If you do have multiple decks, go with Mark Cidade’s answer.