[C#] LINQ to XML d'un XML variable
Voici mon code actuel:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| XDocument xmlDoc = XDocument.Load("test.txt");
// Artist name
var artistq = from artist in xmlDoc.Descendants("artist")
select new
{
Name = artist.Element("name").Value,
Join = artist.Element("join").Value,
};
foreach (var artist in artistq)
{
whatDescription.Text += "[artist]" + artist.Name + "[/artist] ";
} |
Voici le XML en question
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <resp stat="ok" version="1.0" requests="25">
<release id="1930805" status="Accepted">
<artists>
<artist>
<name>David Guetta</name>
<join>Feat.</join>
</artist>
<artist>
<name>Akon</name>
</artist>
</artists>
</release>
</resp> |
Lorsque j'exécute, le programme plante à cause du "Join = artist.Element("join").Value,", car parmi les deux artistes, il n'y a qu'un seul élément "join".
Comment puis-je vérifier avant si le join existe ou non?
Merci.