2014-10-28 69 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