Reference to undeclared namespace prefix when parsing MSXML

泪湿孤枕 提交于 2019-12-02 22:55:13

Using SelectionNamespaces is the correct approach, you are just missing a namespace.

Notice that your XML document explicitly sets the default namespace as follows:

xmlns="http://purl.org/rss/1.0/"

This means that any element without a prefix, such as the item element, is actually in the default namespace. So if you want to select that element with an XPath expression, you must first set an appropriate selection namespace.

To do this, you can change your call to setProperty like so:

doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');

Here you've assigned the default namespace from the document to the rss: prefix in your XPath expression. With that change in place, the following XPath expression should work correctly:

nodes = doc.selectNodes('/rdf:RDF/rss:item/cb:statistics/cb:exchangeRate/cb:targetCurrency');

It works because it references the item element using the correct namespace. The fact that the prefix differs between the XPath expression and the original document is immaterial. It is the namespace which the prefix is bound to that matters.

doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');

Dont forget to load the xsd file or schema to the xmldoc object

is the way to go

I dont have enough reputation to comment. But that bit there saved me a lot of time.

Thank you so much

murrayju

If you are using XMLSerializer and see this error, it is likely that you are running into the IE bug described here:

https://stackoverflow.com/a/11399681

It took me a lot of time to realize that this was happening, so I thought it best to link these two issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!