I have this simple XML file :
<?xml version="1.0" encoding="utf-8" ?>
<Artists>
<artist artistId="1">
<name>ManyBass</name>
<genre>Electronic</genre>
<album>Fireblue</album>
<player>
<song path="Fireblue.mp3"/>
<song path="Porthole.mp3"/>
</player>
</artist>
</Artists>
I want to retrieve all song path to add it in a ListBox like this :
XDocument loaded = XDocument.Load(path);
var q = from c in loaded.Descendants("player")
select (string)c.Element("song path");
foreach (string track in q)
{
myList.Items.Add(track);
}
But my program crash because the track string launch a SystemNullExceptionError. Can someone help me to retrieve properly the attribute :
song path
from my XML file ? Thanks for your help.
A MVVM styled approach would have you bind your ListBox directly to your XML document so that you don’t have to set the contents of the ListBox in the back code.
XAML: