Microsoft XML SDK 2.6 - XML Reference

IXMLDOMSelection Interface

Represents the list of nodes that match a given XSL Pattern or XPath expression.

This interface inherits from IXMLDOMNodeList.

Methods

clone Produces an exact copy of the current XMLDOMSelection object.
matches Checks whether the passed-in node is contained in the current collection.
context Specifies the node (subtree) to apply a selection.
expr Gets or puts the XPath or XSLPattern expression string.
getProperty Looks up a property by name.
peekNode Gets the next node that nextNode will return without advancing the line position.
removeAll Removes all the nodes from the current context that match the query.
removeNext Removes the next node that would be returned by peekNode or nextNode.

Remarks

A new XMLDOMSelection object is created via selectNodes on XMLDOMDocument2. The selectNodes method still returns an XMLDOMNodeList pointer, so in Visual Basic (and C++) you have to cast the result of selectNodes into an IXMLDOMSelection, as follows:

Dim Selection As IXMLDOMSelection
Set Selection = xmldoc.selectNodes("//Customer[Zip =98072]")

The XMLDOMSelection object inherits the threading model of the document that created it.

This interface is an extension of the W3C DOM.

Example

BOOL DOMSelectionDemo()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   IXMLDOMSelection *pIXMLDOMSelection=NULL;
   IXMLDOMNodeList *pIXMLDOMNodeList=NULL;
   IXMLDOMNode *pIXMLDOMNode=NULL;
   IXMLDOMDocument2 *pIXMLDOMDocument2=NULL;
   BSTR bstrValue;
   HRESULT hr;

   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\\samplexmldtd.xml")), &sResult);
            if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
            {
               hr=pIXMLDOMDocument2->selectNodes( 
               _T("*/BOOK[TITLE='Cosmos']"), &pIXMLDOMNodeList);
               if(SUCCEEDED(hr))
               {
                  hr=pIXMLDOMNodeList->QueryInterface(IID_IXMLDOMSelection
                     ,(void**)&pIXMLDOMSelection );
                  if(SUCCEEDED(hr) && pIXMLDOMSelection)
                  {
                     LONG uLength;

                     bResult=TRUE;
                     hr=pIXMLDOMSelection->get_length(&uLength);
                     if(SUCCEEDED(hr))
                     {
                        for(int iIndex=0; iIndex < uLength; iIndex++)
                        {
                           // remove all the nodes from the list-display 
                           // them as they are removed.
                           hr=pIXMLDOMSelection->removeNext(
                              &pIXMLDOMNode);
                           if(SUCCEEDED(hr) && pIXMLDOMNode)
                           {   
                              hr=pIXMLDOMNode->get_text(&bstrValue);
                              if(SUCCEEDED(hr))
                                 ::MessageBox(NULL, bstrValue, _T("Node
                                  Text"), MB_OK);
                              RELEASE(pIXMLDOMNode);
                           }
                        }
                     }
                     RELEASE(pIXMLDOMSelection);
                  }
                  RELEASE(pIXMLDOMNodeList);
               }
            }
         }
         RELEASE(pIXMLDOMDocument2);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXMLDOMNode);
      CHECK_AND_RELEASE(pIXMLDOMDocument2);
      CHECK_AND_RELEASE(pIXMLDOMNodeList);
      CHECK_AND_RELEASE(pIXMLDOMSelection);
      DisplayErrorToUser();
   }
   return bResult;
}

d:\\inetpub\\wwwroot\\samplexmldtd.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>
 <BOOK>
    <TITLE>Home Town</TITLE>
    <AUTHOR>Tracy Kidder</AUTHOR>
    <PUBLISHER>Random House</PUBLISHER>
 </BOOK>
</COLLECTION>

Output (in a message box)

Cosmos Carl Sagan Ballantine Books

Requirements

Implementation: Msxml2.dll

Header and IDL Files: Msxml.h, Xmldom.idl

Version-Independent ProgID: Msxml2.DOMDocument, Msxml2.FreeThreadedDOMDocument

Version-Independent CLSID: f6d90f11-9c73-11d3-b32e-00c04f990bb4

Version-Dependent ProgID: Msxml2.DOMDocument.2.6, Msxml2.FreeThreadedDOMDocument.2.6

Version-Dependent CLSID: f5078f1b-c551-11d3-89b9-0000f81fe221

See Also

Examples