2015-02-23 102 views
0

我應該配置到讀谷歌購物飼料什麼的命名空間來選擇的節點?然後,如何使用ASP.NET選擇一個值? 我似乎無法選擇值節點g:google_product_categoryASP.NET無法使用selectSingleNode方法與命名空間管理

我的谷歌飼料(某些節點省略概述)

<?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>  

我的代碼

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?! 

回答

1

productXML.DocumentElement.NamespaceURI不對應於「http://base.google.com/ns/1.0」,因此將其設置爲

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

或使用node.Item

Dim productXML As New XmlDocument 
Dim node As XmlNode 

productXML.LoadXml(responseString) 

Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(productXML.NameTable) 
mgr.AddNamespace("g", productXML.DocumentElement.NamespaceURI) 

Dim root As XmlElement = productXML.DocumentElement 
Dim nodeList As XmlNodeList = root.SelectNodes("/rss/channel/item", mgr) 

For Each node In nodeList 
    If node.Item("g:google_product_category") IsNot Nothing Then 
     Console.WriteLine(node.Item("g:google_product_category").InnerXml) 
    End If 
Next