Results 1 to 8 of 8

Thread: [RESOLVED] Reading XML

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    Resolved [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.

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    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

  3. #3
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    419

    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)

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,553

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    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

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,304

    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

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,326

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    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
  •  



Click Here to Expand Forum to Full Width