MSXML Tips

02 May 2001 17:23

XML Everywhere

Beginners' Guide to the DOM

Importing MSXML Definitions in VC++ 6.0

#import "msxml.dll" named_guids no_implementation raw_interfaces_only
using namespace MSXML;

Capturing Validation Errors

In order to "see" a validation error, you must set the Async property to VARIANT_FALSE.  Otherwise the load() method will succeed by setting the "success" parameter to VARIANT_TRUE.

IXMLDOMNode::appendChild / insertBefore / removeChild

These two methods can have unexpected results.  If you move a node from one document to another, MSXML performs several operations to make sure the node makes "sense" in its new home, including:

  1. Adding namespace declarations where appropriate
  2. Removing attributes that were instantiated via DTD defaults.  This happens when the source document has a DTD and the target document either doesn't have a DTD or the DTD doesn't have particular attributes

Item #2 completely surprised me.  Seeing attributes disappear was alarming and looked like a bug.  But no, it's as-designed.  One way to avoid this problem is to convert the source document to a string and re-parse it without the DTD declaration.  Slow but effective.

More details are here

Passing MSXML Objects Across Apartments

See here

Mixing Threading Models

You can not move a node from a neutral-threaded DOM (Micorosft.XMLDOM) into a free-threaded DOM (Microsoft.FreeThreadedXMLDOM) and vice-versa.  You must reparse the source document using the same thread model as the target document.  The error is 0x80004005 XMLOM_INVALID_MODEL.

Similarly, you can not transform a neutral-threaded DOM using a stylesheet that's in a free-threaded DOM and vice-versa.

Using XPATH in selectNodes, selectSingleNode

If you want MSXML3 to use XPATH instead of "MS patterns" in methods like selectNodes, you need to set the SelectionLanguage property:

CComPtr<IXMLDOMDocument2> idom2;
CComVariant v(L"XPath");
idom2->setProperty(CComBSTR("SelectionLanguage"),v);