Click here to Skip to main content
15,888,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm working at a program which allow users to play p2p streams (sopcast) . I want to make my tv programs list with XML and a listbox.

How can I read all channels from a XML ? and i want if i click a program on listbox to get another informations about it and play the URL in WMP Control

This is the XML file[^]:

XML
<channels>
  <channel id="1">
    <name>TVR1</name>
    <protocol>Acestream</protocol>
    <quality>720p</quality>
    <url>http://tvr1</url>
  </channel>
  <channel id="2">
    <name>Antena 1</name>
    <protocol>Sopcast</protocol>
    <quality>4k</quality>
    <url>http://antena1</url>
  </channel>
  <channel id="3">
    <name>Kanal D</name>
    <protocol>Standard</protocol>
    <quality>TEEEST</quality>
    <url>http://kanald</url>
  </channel>
  <channel id="4">
    <name>Radio Pro FM</name>
    <protocol>FMTr</protocol>
    <quality>hd</quality>
    <url>radio</url>
  </channel>
</channels>




Look: http://p2ptv.ml/preview/chlist.png[^]

I can read all channels (in XML) but i want when i click a random channel to get on 3 labels all rest of informations about the selected channel (< protocol > ,< quality > and < url > ) How can I get this without write all channels in code? I want to add/remove channels ONLY by XML, not by code.

Sorry for my bad english, but i'm still learning english.
if you don't understand something, please tell me.

What I have tried:

I tried to parse all channels called by < name > in XML.
This worked, but i don't know how to get another informations when i click on a channel in ListBox control like < protocol > , < quality > and < url > .
Posted
Updated 1-Aug-16 0:58am
v4
Comments
Maciej Los 31-Jul-16 15:04pm    
"I want to add/remove channels ONLY by XML, not by code" - Not sure what you mean...
Member 12296756 31-Jul-16 16:36pm    
i don't want so save channels in my project code.
i want a code which detect automatically number of channels.
i realised this, but i don't know how to get the rest of informations about the selected channel in listbox

1 solution

There's few ways to achieve that using code:

1)
The best option for you is to use custom class plus Xml Serialization/Deserialization[^]:
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]

2)
Another option is to use Linq to XML[^].
VB.NET
Dim xcontent As String = "<channels>" & _
	"  <channel id='1'>" & _
	"    <name>TVR1</name>" & _
	"    <protocol>Acestream</protocol>" & _
	"    <quality>720p</quality>" & _
	"    <url>http://tvr1</url>" & _
	"  </channel>" & _
	"  <channel id='2'>" & _
	"    <name>Antena 1</name>" & _
	"    <protocol>Sopcast</protocol>" & _
	"    <quality>4k</quality>" & _
	"    <url>http://antena1</url>" & _
	"  </channel>" & _
	"  <channel id='3'>" & _
	"    <name>Kanal D</name>" & _
	"    <protocol>Standard</protocol>" & _
	"    <quality>TEEEST</quality>" & _
	"    <url>http://kanald</url>" & _
	"  </channel>" & _
	"  <channel id='4'>" & _
	"    <name>Radio Pro FM</name>" & _
	"    <protocol>FMTr</protocol>" & _
	"    <quality>hd</quality>" & _
	"    <url>radio</url>" & _
	"  </channel>" & _
	"</channels>"

	Dim xdoc As XDocument = XDocument.Parse(xcontent)
	
	Dim chanel2find = "TVR1"
	
	Dim result = xdoc.Descendants("channel") _
					.Where(Function(x) x.Element("name") = chanel2find) _
					.Select(Function(x) New With _
						{ _
							.id = x.Attribute("id").Value, _
							.name = x.Element("name").Value, _
							.protocol = x.Element("protocol").Value, _
							.quality = x.Element("quality").Value, _
							.url = x.Element("url").Value _
						}) _
					.SingleOrDefault()
	Console.WriteLine("{0}", result.protocol)


3)
Another option is to use custom class + Linq to XML:
class definition:
VB.NET
Public Class Channel
	Private iid As Integer = 0
	Private sname As String = String.Empty
	Private sprotocol As String = String.Empty
	Private squality As String = String.Empty
	Private surl As String = String.Empty
	
	Public Sub New
		'default constructor
	End Sub
	
	Public Sub New (ByVal _id As Integer, ByVal _nam As String, ByVal _prot As String, _
					ByVal _qty As String, ByVal _url As String)
		iid = _id
		sname = _nam
		sprotocol = _prot
		squality = _qty
		surl = _url
	End Sub
	
	Public Property ID As Integer
		Get
			Return iid
		End Get
		Set (value As Integer)
			iid = value
		End Set
	End Property

	Public Property Name As String
		Get
			Return sname
		End Get
		Set (value As String)
			sname = value
		End Set
	End Property

	Public Property Protocol As String
		Get
			Return sprotocol
		End Get
		Set (value As String)
			sprotocol = value
		End Set
	End Property

	Public Property Quality As String
		Get
			Return squality
		End Get
		Set (value As String)
			squality = value
		End Set
	End Property

	Public Property Url As String
		Get
			Return surl
		End Get
		Set (value As String)
			surl = value
		End Set
	End Property
	
End Class


Usage:
VB.NET
'load data from xml to list of channels
	Dim channels As List(Of Channel) =  New List(Of Channel)
	channels = xdoc.Descendants("channel") _
					.Select(Function(x) New Channel With _
						{ _
							.ID = x.Attribute("id").Value, _
							.Name = x.Element("name").Value, _
							.Protocol = x.Element("protocol").Value, _
							.Quality = x.Element("quality").Value, _
							.Url = x.Element("url").Value _
						}) _
					.ToList()
        'get single channel 
	Dim c As Channel = channels.Cast(Of Channel) _
			.Where(Function(x) x.Name = channel2find) _
			.SingleOrDefault()
	'display name and protocol		
	Console.WriteLine("{0} -> {1}", c.Name, c.Protocol)


Try!
Good Luck!
 
Share this answer
 
Comments
Member 12296756 1-Aug-16 10:36am    
the code works, but how can I do this in windows forms ?
Maciej Los 1-Aug-16 10:54am    
In the same way as it is used in console application. Instead of Console.WriteLine("{0} -> {1}", c.Name, c.Protocol), use Me.TextBox1.Text = c.Protocol
Member 12296756 1-Aug-16 10:56am    
ok. i will try now.
thanks.
Member 12296756 1-Aug-16 11:19am    
but...there is a problem.
i try to get info when click a item on listbox.
i modified Dim channel2find = "TVR1" into Dim channel2find = channelsList.SelectedItem but i get an error.
Member 12296756 1-Aug-16 11:23am    
i fixed this by adding Dim function in SelectedChangeIndex of the listbox
it works great.
thanks man!
i will put credits for this fix in my program!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900