2014-10-28 67 views
1

我有这样的XML:XML命名空间,不能选择节点

<?xml version="1.0" encoding="utf-8" ?> 
    <ArrayOfFileInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://tempuri.org/"> <Data> ...... 

当我在XML离开"xmlns="http://tempuri.org/",下面不返回任何节点:

CString tag = L"//Data"; 
MSXML2::IXMLDOMNodeListPtr pDataTag = pXMLDocument->selectNodes((_bstr_t)tag); 

当我删除了属性,但一切工作正常。

你能帮我吗?

回答

2

为了在XPath中选择名称空间节点,您需要使用XPath库提供的工具将前缀绑定到相关名称空间URI,然后在按名称选择节点时使用该前缀。为SelectNodes方法的MSDN文档有a C++ example of just this,关键是要设置文档

pXMLDocument->setProperty("SelectionNamespaces", "xmlns:tmp='http://tempuri.org/'"); 

然后将允许您使用//tmp:Data

+0

致谢的XPath的​​属性,多数民众赞成它。 – lalelu 2014-10-28 12:58:03

+0

@lalelu如果这个答案解决了你的问题,那么请[**接受它作为答案**](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。谢谢! – 2014-10-28 22:19:17