How to pull a URL from stations.xml, to play in the ListBox (MouseDoubleClick)?
my code
XAML
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="220" Margin="20,30,0,0" VerticalAlignment="Top" Width="150" MouseDoubleClick="mylistbox"/>
</Grid>
</Window>
Here is my Xml file stations.xml
<?xml version="1.0" encoding="utf-8"?>
<stations>
<station url="http://onair.eltel.net:80/europaplus-128k" id="0">EuropaPlus2</station>
<station url="http://online.radiorecord.ru:8101/rr_128" id="1">RRadio</station>
<station url="http://radio.kazanturl-fm.ru:8000/mp3" id="2">Kazanturl</station>
<station url="http://stream.kissfm.ua:8000/kiss" id="3">Kiss FM</station>
</stations>
ะก# code enter link description here
You are populating your ListBox with strings which does not include the "url" attribute. One option is to change your LoadStations() method to add the entire XmlNode to the ListBox item. The inner text (station name) will be displayed by default.
Then, you can extract the "url" attribute in your MouseDoubleClick handler like this:
Also, if possible, you may want to consider loading your ListBox items from an XML file using WPF data binding. You would be able to remove your LoadStations() method by binding the ListBox to the XML file in XAML. Something like this: