IXMLDOMDocument *pXMLDoc = NULL; |
IXMLDOMNode *pNode = NULL; |
BSTR bstr = NULL; |
VARIANT_BOOL status; |
VARIANT var; |
HRESULT hr; |
int i = 0; |
|
CoInitialize(NULL); |
VariantInit(&var); |
|
hr = CoCreateInstance(CLSID_DOMDocument, NULL, |
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER |
, IID_IXMLDOMDocument, (LPVOID*)&pXMLDoc); |
if (!pXMLDoc || pXMLDoc==NULL) { |
iItems = -1; |
// DOM creation failed! |
} |
|
VariantClear(&var); |
var = bstr2var(_bstr_t("d:\\book.xml")); |
hr = pXMLDoc->load(var, &status); |
if (status!=VARIANT_TRUE) { |
iItems = -2; |
// unable to load file! |
} |
|
pXMLDoc->selectSingleNode(L"catalog/book[last()]/author", &pNode); |
|
if (pNode!=NULL) { |
pNode->get_text(&bstr); |
CString u; |
u.Format("%s", (LPCTSTR)(_bstr_t)bstr); |
AfxMessageBox(u); |
} else { |
AfxMessageBox("pNode = NULL!"); |
} |
The result of above expression is pNode = NULL.
I use VC++ 6.0 and MSXML2 (just curious, I also tried to use VC 2005 and MSXML6 with similar code as above, but the result is the same)
Did I miss something?
Thanks!
-nb: I test the expression using BIT-101 (http://www.bit-101.com/xpath/) and works fine.