ASP.NET 无法 select 将 SelectSingleNode 方法与名称空间管理器结合使用
ASP.NET unable to select node using SelectSingleNode method with namespace manager
我应该配置什么命名空间来读取 Google 购物提要?然后如何使用 ASP.NET select 一个值?
我似乎无法 select 节点 g:google_product_category
的值
我的 Google 提要(为了概述省略了一些节点)
<?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?!
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
我应该配置什么命名空间来读取 Google 购物提要?然后如何使用 ASP.NET select 一个值?
我似乎无法 select 节点 g:google_product_category
我的 Google 提要(为了概述省略了一些节点)
<?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?!
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