Click here to Skip to main content
14,449,715 members
Rate this:
Please Sign up or sign in to vote.
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[^]:

<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 1: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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^].
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:
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:
'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!
   
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!!
Maciej Los 1-Aug-16 12:02pm
   
I'm very glad i can help you. Can you accept my answer (green button) to remove your question from unanswered list?
Member 12296756 1-Aug-16 12:26pm
   
Done;)
Member 12296756 1-Aug-16 11:29am
   
one more question> how can I use URL instead of copy XML directly in code?
Maciej Los 1-Aug-16 12:01pm
   
It depends on many factors. Does the xml file is stored locally or you access to it via service?
Member 12296756 1-Aug-16 12:24pm
   
I want to access it from a website.
http://p2ptv.ml/preview/ro_channels.xml - this is the url to XML file
Maciej Los 1-Aug-16 12:49pm
   
Use WebRequest:
Dim myUrl As String = "http://p2ptv.ml/preview/ro_channels.xml"
Dim request = WebRequest.Create(myUrl)
request.Timeout = 5000
Dim response = request.GetResponse()

Using stream = response.GetResponseStream()
Dim xdoc = XDocument.Load(stream)
'now you can use xdoc variable
End Using


More here: http://stackoverflow.com/questions/6783697/how-do-i-read-xml-from-internet-in-visual-basic-net
Member 12296756 1-Aug-16 14:16pm
   
i get declaration expected with this code > request.Timeout = 5000
Member 12296756 1-Aug-16 14:30pm
   
well, i removed it and it works fine now.
thanks!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100