locked
Issue parsing xml due to possible namespace? RRS feed

  • Question

  • Hey all, I am fairly new to XML documents and managed to get a weather xml parsed using openweathermaps api. However, now I am trying to parse a different kind of xml that contains a namespace, I think. Code and XML below.

    XML Returned from URL

    <?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
    
    <!--
    This atom/xml feed is an index to active advisories, watches and warnings 
    issued by the National Weather Service.  This index file is not the complete 
    Common Alerting Protocol (CAP) alert message.  To obtain the complete CAP 
    alert, please follow the links for each entry in this index.  Also note the 
    CAP message uses a style sheet to convey the information in a human readable 
    format.  Please view the source of the CAP message to see the complete data 
    set.  Not all information in the CAP message is contained in this index of 
    active alerts.
    -->
    
    <feed
    xmlns = 'http://www.w3.org/2005/Atom'
    xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
    xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
    >
    <!-- TZN = <> -->
    <!-- TZO = <> -->
    <!-- http-date = Sat, 01 Jan 2019 18:55:06 GMT -->
    <id>https://alerts.weather.gov/cap/wwaatmget.php?x=XXXXXX&amp;y=0</id>
    <generator>NWS CAP Server</generator>
    <updated>2019-01-26T18:55:06+00:00</updated>
    <author>
    <name>w-nws.webmaster@noaa.gov</name>
    </author>
    <title>Current Watches, Warnings and Advisories for Morrison (XXXXXX) Minnesota Issued by the National Weather Service</title>
    <link href="https://alerts.weather.gov/cap/wwaatmget.php?x=XXXXXX&amp;y=0"/>
    
      	<entry>
      	<id>https://alerts.weather.gov/cap/wwaatmget.php?x=XXXXXX&amp;y=0</id>
      	<updated>2019-01-26T18:55:06+00:00</updated>
      	<author>
      	<name>w-nws.webmaster@noaa.gov</name>
      	</author>
      	<title>There are no active watches, warnings or advisories</title>
      	<link href="https://alerts.weather.gov/cap/wwaatmget.php?x=XXXXXX&amp;y=0"/>
      	</entry>
      	</feed>
      	

    Code Attempt

        Public Sub noaa()
            Dim useragent As String = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.17 Safari/537.11"
            Dim NOAArequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(My.Settings.noaalink)
            NOAArequest.UserAgent = useragent
            Dim NOAAresponse As System.Net.HttpWebResponse = NOAArequest.GetResponse()
            If NOAAresponse.StatusCode = System.Net.HttpStatusCode.OK Then
                Dim NOAAstream As System.IO.Stream = NOAAresponse.GetResponseStream()
                Dim NOAAreader As New System.IO.StreamReader(NOAAstream)
                Dim NOAAcontents As String = NOAAreader.ReadToEnd()
                Dim NOAAdocument As New System.Xml.XmlDocument()
                NOAAdocument.LoadXml(NOAAcontents)
    
                Dim NOAAIFpath As String = "/feed/entry/title"
                Dim NOAAIFnode As XmlNode = NOAAdocument.SelectSingleNode(NOAAIFpath)
                Dim NOAAIF As String = NOAAIFnode.InnerText
    
                MsgBox(NOAAIF)
    
            End If
        End Sub

    I am trying to return the first /feed/entry/title innertext. When I run this code however I get this error System.NullReferenceException: 'Object reference not set to an instance of an object.' This is what leads me to believe it has something to do with namespace. How do I use a namespace and what is the namespace providing??

    Saturday, January 26, 2019 7:07 PM

Answers

  • Check this:

    Dim nm = New XmlNamespaceManager(NOAAdocument.NameTable)
    nm.AddNamespace("r", "http://www.w3.org/2005/Atom")
    Dim NOAAIFpath As String = "/r:feed/r:entry/r:title"
    Dim NOAAIFnode As XmlNode = NOAAdocument.SelectSingleNode(NOAAIFpath, nm)
    Dim NOAAIF As String = NOAAIFnode.InnerText
    

    • Marked as answer by k7s41gx Saturday, January 26, 2019 7:45 PM
    Saturday, January 26, 2019 7:33 PM

All replies

  • Check this:

    Dim nm = New XmlNamespaceManager(NOAAdocument.NameTable)
    nm.AddNamespace("r", "http://www.w3.org/2005/Atom")
    Dim NOAAIFpath As String = "/r:feed/r:entry/r:title"
    Dim NOAAIFnode As XmlNode = NOAAdocument.SelectSingleNode(NOAAIFpath, nm)
    Dim NOAAIF As String = NOAAIFnode.InnerText
    

    • Marked as answer by k7s41gx Saturday, January 26, 2019 7:45 PM
    Saturday, January 26, 2019 7:33 PM
  • Thanks Viorel_. This is working great, although I don't quite understand why the "VAR" is required for this. Can you explain a little bit more?
    Saturday, January 26, 2019 7:46 PM