Gets the node (subtree) that is applied to the selection.
Set objXMLDOMNode = objXMLDOMSelection.context
HRESULT get_context(IXMLDOMNode ** ppNode);
Because selectNodes can be called on any node type, context can also be assigned any node type. A node from a different document than the one that originally created this XMLDOMSelection object can also be specified as long as it has the same threading model. Calling context also resets the state of the node list so that nextNode starts over.
The following Visual Basic example shows what the context of a selection contains when a query is executed and also that the selected set is reset when this property is changed:
Dim xmlDoc As New MSXML2.DOMDocument Dim selection As MSXML2.IXMLDOMSelection xmlDoc.loadXML ("<Customer>Microsoft</Customer>") xmlDoc.setProperty "SelectionLanguage", "XPath" Set selection = xmlDoc.selectNodes("//Customer") MsgBox selection.context.xml
Selection.context.xml contains "<Customer>Amit</Customer>", selection.length = 1
xmlDoc.loadXML ("<new>none</new>") MsgBox selection.context.xml
Selection.context.xml contains "<new>no-one</new>", selection.length = 0
Specifies the node (subtree) to apply to the selection.
objXMLDOMSelection.context = objXMLDOMNode
HRESULT putref_context(IXMLDOMNode * pNode);
Because selectNodes can be called on any node type, context can also be assigned any node type. A node from a different document than the one that originally created this XMLDOMSelection object can also be specified as long as it has the same threading model. Calling context also resets the state of the node list so that nextNode starts over.
The following C++ code example shows what the context property contains after selectNodes is executed.
BOOL DOMSelectionContextDemo() { BOOL bResult = FALSE; short sResult = FALSE; IXMLDOMSelection *pIXMLDOMSelection=NULL; IXMLDOMNode *pIXMLDOMNode=NULL; IXMLDOMDocument2 *pIXMLDOMDocument2=NULL; HRESULT hr; BSTR bstrValue; try { hr=CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER, IID_IXMLDOMDocument2, (LPVOID*)(&pIXMLDOMDocument2)); SUCCEEDED(hr) ? 0 : throw hr; if(pIXMLDOMDocument2) { hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE); if(SUCCEEDED(hr)) { hr=pIXMLDOMDocument2->load(_variant_t( _T("d:\\inetpub\\wwwroot\\samplexml.xml")), &sResult); if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE)) { hr=pIXMLDOMDocument2->selectNodes( _T("*/BOOK[TITLE='Cosmos']"), (IXMLDOMNodeList**)&pIXMLDOMSelection); if(SUCCEEDED(hr)) { if(SUCCEEDED(hr) && pIXMLDOMSelection) { hr=pIXMLDOMSelection->get_context(&pIXMLDOMNode); if(SUCCEEDED(hr) && pIXMLDOMNode) { hr=pIXMLDOMNode->get_xml(&bstrValue); if(SUCCEEDED(hr)) ::MessageBox(NULL, bstrValue, _T("Current Selection Context"), MB_OK); bResult=TRUE; RELEASE(pIXMLDOMNode); } RELEASE(pIXMLDOMSelection); } } } } RELEASE(pIXMLDOMDocument2); } } catch(...) { CHECK_AND_RELEASE(pIXMLDOMNode); CHECK_AND_RELEASE(pIXMLDOMSelection); CHECK_AND_RELEASE(pIXMLDOMDocument2); DisplayErrorToUser(); } return bResult; }
d:\\inetpub\\wwwroot\\samplexml.xml
<?xml version='1.0'?> <COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes"> <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE> <BOOK> <TITLE>Cosmos</TITLE> <AUTHOR>Carl Sagan</AUTHOR> <PUBLISHER>Ballantine Books</PUBLISHER> </BOOK> <BOOK> <TITLE>Catwings</TITLE> <AUTHOR>Ursula K. Le Guin</AUTHOR> <PUBLISHER>Scholastic</PUBLISHER> </BOOK> </COLLECTION>
Output (in a message box)
<?xml version='1.0'?> <COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes"> <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE> <BOOK> <TITLE>Cosmos</TITLE> <AUTHOR>Carl Sagan</AUTHOR> <PUBLISHER>Ballantine Books</PUBLISHER> </BOOK> <BOOK> <TITLE>Catwings</TITLE> <AUTHOR>Ursula K. Le Guin</AUTHOR> <PUBLISHER>Scholastic</PUBLISHER> </BOOK> </COLLECTION>