-
Oct 23rd, 2020, 09:28 PM
#1
Thread Starter
Member
[RESOLVED] Reading XML
I am having no problem reading the XML file and pulling out what I need but the issue I am having is when what I need is not there when the xml updated.
status file = status.xsl
For example. Reading status file and looking for this:
Code:
<icecastst>
<Not-Connected>1</Not-Connected>
</icecastst>
When stream comes online the status file changes to something like this.
Code:
<icecastst>
<Mount-Point>/listen.mp3</Mount-Point>
<Stream-Title>Christmas Broadcast 2020</Stream-Title>
<Stream-Description>Christmas Music Broadcast 24/7</Stream-Description>
<Content-Type>audio/mpeg</Content-Type>
<Mount-started>23/Oct/2020:20:45:59 -0500</Mount-started>
<Bitrate>64</Bitrate>
<Current-Listeners>0</Current-Listeners>
<Peak-Listeners>0</Peak-Listeners>
<Stream-Genre>Various</Stream-Genre>
<Stream-URL>http://www.christmasbroadcast.com</Stream-URL>
<ice-bitrate>64</ice-bitrate>
<icy-info>ice-samplerate=44100;ice-bitrate=64;ice-channels=2</icy-info>
<Current-Song/>
</icecastst>
This is what I currently have but getting this error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Code:
Dim strNotOnline As String
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument
m_xmld.Load(xmlloc)
m_nodelist = m_xmld.GetElementsByTagName("icecastst")
For Each m_node In m_nodelist
If (m_node("Not-Connected").InnerXml <> Nothing) Then
LiveCheck.Text = "Live"
Else
LiveCheck.Text = "Stream Offline"
End If
Next
Unsure how to have it check if Not-Connected is there as the node item.
-
Oct 24th, 2020, 07:38 PM
#2
Thread Starter
Member
Re: Reading XML
Any ideas how I can fix this. Tried this but still no good.
Code:
For Each m_node In m_nodelist
If m_node IsNot Nothing Then
LiveCheck.Text = "Live"
Else
LiveCheck.Text = "Stream Offline"
End If
Next
-
Oct 25th, 2020, 04:04 AM
#3
Re: Reading XML
If m_nodelist contain something between <icecastst> and </icecastst> when it is live and nothing when it is not, then check if the list is not empty : m_nodelist.count <> 0
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Oct 25th, 2020, 09:26 AM
#4
Re: Reading XML
If you do need to check the items, a re-arrangement of the loop (so you set Offline first, and in the loop only set Live) is a better idea:
Code:
LiveCheck.Text = "Stream Offline"
For Each m_node In m_nodelist
If m_node IsNot Nothing Then
LiveCheck.Text = "Live"
Exit For
End If
Next
-
Oct 25th, 2020, 06:43 PM
#5
Thread Starter
Member
Re: Reading XML
Thanks but when the stream is changes from offline to online:
status.xml
Offline
Code:
<icecastst>
<Not-Connected>1</Not-Connected>
</icecastst>
Online (example)
Code:
<icecastst>
<Mount-Point>/listen.mp3</Mount-Point>
<Stream-Title>Christmas Broadcast 2020</Stream-Title>
<Stream-Description>Christmas Music Broadcast 24/7</Stream-Description>
<Content-Type>audio/mpeg</Content-Type>
<Mount-started>23/Oct/2020:20:45:59 -0500</Mount-started>
<Bitrate>64</Bitrate>
<Current-Listeners>0</Current-Listeners>
<Peak-Listeners>0</Peak-Listeners>
<Stream-Genre>Various</Stream-Genre>
<Stream-URL>http://www.christmasbroadcast.com</Stream-URL>
<ice-bitrate>64</ice-bitrate>
<icy-info>ice-samplerate=44100;ice-bitrate=64;ice-channels=2</icy-info>
<Current-Song/>
</icecastst>
Status does not change
Code:
Private Sub StreamOnline()
xmlloc = "http://localhost/status.xml"
Dim strNotOnline As String
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument
m_xmld.Load(xmlloc)
m_nodelist = m_xmld.GetElementsByTagName("icecastst")
For Each m_node In m_nodelist
If m_node IsNot Nothing Then
LiveCheck.Text = "Live"
Console.WriteLine(m_node)
Else
LiveCheck.Text = "Stream Offline"
Console.WriteLine(m_node)
End If
Next
End Sub
-
Oct 25th, 2020, 07:41 PM
#6
Re: Reading XML
It's pretty much as Si said in Post #4:
Code:
LiveCheck.Text = "Stream Offline"
For Each m_node In m_nodelist
If m_node("Not-Connected") Is Nothing Then
LiveCheck.Text = "Live"
Exit For
End If
Next
Sets status to "OffLine" initially, then checks for presence of <Not-Connected> node.
If <Not-Connected> node does not exist (Is Nothing), sets status to "Live" and stops checking (i.e. exits loop).
If <Not-Connected> node can be present but with a value of 0 (False) then will fail.
If there actually are multiple <icecastst> nodes, then that's just confusing! ;-p
-
Oct 26th, 2020, 08:33 AM
#7
Re: Reading XML
I'd use XElement and LINQ. In the example I've used XML literals but did provide the code for reading from a URI.
Code:
Dim ie As IEnumerable(Of XElement)
Dim xe As XElement
'production - from URI
' xe = XElement.Load("http://localhost/status.xml")
'test - use literal
xe = <icecastst>
<Mount-Point>/listen.mp3</Mount-Point>
<Stream-Title>Christmas Broadcast 2020</Stream-Title>
<Stream-Description>Christmas Music Broadcast 24/7</Stream-Description>
<Content-Type>audio/mpeg</Content-Type>
<Mount-started>23/Oct/2020:20:45:59 -0500</Mount-started>
<Bitrate>64</Bitrate>
<Current-Listeners>0</Current-Listeners>
<Peak-Listeners>0</Peak-Listeners>
<Stream-Genre>Various</Stream-Genre>
<Stream-URL>http://www.christmasbroadcast.com</Stream-URL>
<ice-bitrate>64</ice-bitrate>
<icy-info>ice-samplerate=44100;ice-bitrate=64;ice-channels=2</icy-info>
<Current-Song/>
</icecastst>
ie = From el In xe...<Not-Connected> Select el Take 1
If ie.Count = 1 Then
'not connected
Stop
Else
'connected
Stop
End If
xe = <icecastst>
<Not-Connected>1</Not-Connected>
</icecastst>
ie = From el In xe...<Not-Connected> Select el Take 1
If ie.Count = 1 Then
'not connected
Stop
Else
'connected
Stop
End If
Stop
In production it should look like this, where the Stop's are replaced as required.
Code:
Dim ie As IEnumerable(Of XElement)
Dim xe As XElement
'production - from URI
xe = XElement.Load("http://localhost/status.xml")
ie = From el In xe...<Not-Connected> Select el Take 1
If ie.Count = 1 Then
'not connected
Stop
Else
'connected
Stop
End If
-
Oct 28th, 2020, 07:11 PM
#8
Thread Starter
Member
Re: Reading XML
Thank you. Did what I needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|