locked
ASP.NET unable to select node using SelectSingleNode method with namespace manager RRS feed

  • Question

  • User-1246604461 posted

    What namespace should I configure to read Google shopping feed? And then how do I select a value using ASP.NET? I can't seem to select the value for node g:google_product_category

    My Google feed (some nodes omitted for overview)

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
        <channel>
            <title><![CDATA[Foo]]></title>
            <link>http://www.foo.com/</link>
            <description><![CDATA[]]></description>
            <item>
                <g:id>10580119</g:id>
                <title><![CDATA[Some title]]></title>
                <g:google_product_category><![CDATA[Bags]]></g:google_product_category>
            </item>
        </channel>
    </rss>      

    My code

    Dim productXML As New XmlDocument
    Dim root As XmlNode
    Dim node As XmlNode
    
    productXML.LoadXml(responseString) 'responseString contains the full Google feed
    Dim nodeList As XmlNodeList = root.SelectNodes("/rss/channel/item")
    
    Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(productXML.NameTable)
    mgr.AddNamespace("g", productXML.DocumentElement.NamespaceURI)
    
    'here a nodeList.Count call returns the correct number of products, so I'm able to read some items
    
    For Each node In nodeList
    
    If node.SelectSingleNode("g:google_product_category", mgr) IsNot Nothing Then 'what should I add here?!
    Tuesday, February 24, 2015 9:06 AM

Answers

  • User1508394307 posted

    Hi Peter 

    I think you could use 

    If node.Item("g:google_product_category") IsNot Nothing Then  
        // print ...
        node.Item("g:google_product_category").InnerXml)
    End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 24, 2015 10:07 AM

All replies

  • User1508394307 posted

    Hi Peter 

    I think you could use 

    If node.Item("g:google_product_category") IsNot Nothing Then  
        // print ...
        node.Item("g:google_product_category").InnerXml)
    End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 24, 2015 10:07 AM
  • User1508394307 posted

    Actually your code was good but if you test it at run-time you will see that productXML.DocumentElement.NamespaceURI is "" 

    So you need to set namespace uri as

    mgr.AddNamespace("g", "http://base.google.com/ns/1.0")

    and then your code with 

    If node.SelectSingleNode("g:google_product_category", mgr) IsNot Nothing Then

    will work.

    Tuesday, February 24, 2015 12:41 PM