How to pull a URL from stations.xml, to play in the ListBox

111 views Asked by At

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

1

There are 1 answers

0
Steve Wong On BEST ANSWER

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.

public void LoadStations()
{
     string fileName = "data.xml";
     if (File.Exists(fileName))
     {
         XmlDocument xmlDoc = new XmlDocument();

         xmlDoc.Load(fileName);
         XmlNodeList stationNodes = xmlDoc.SelectNodes("//stations/station");
         foreach (XmlNode stationNode in stationNodes)
         {
              listBox.Items.Add(stationNode); // by default, the InnerText will be displayed                
         }

         xmlDoc.Save(fileName); 
    }
    else
    {
         MessageBox.Show(" ");
    }
}

Then, you can extract the "url" attribute in your MouseDoubleClick handler like this:

private void mylistbox(object sender, MouseButtonEventArgs e)
{            
   XmlNode selectedNode = listBox.SelectedItem as XmlNode;
   string url = selectedNode.Attributes["url"].Value;
   MessageBox.Show(url);
   Play(url);
}

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:

<Grid>
  <Grid.Resources>
    <XmlDataProvider x:Key="StationsXml" Source="stations.xml" />  
  </Grid.Resources>

  <ListBox HorizontalAlignment="Left" Height="220" Margin="20,30,0,0" 
       VerticalAlignment="Top" Width="150"            
       ItemsSource="{Binding Source={StaticResource StationsXml}, XPath=stations/station}"
       MouseDoubleClick="mylistbox"
          />
</Grid>