How to set an XML attribute to an integer

Edward discusses how to set an integer in the MSXML APIs in this thread. I wanted to provide the snippet of code necessary to do this on an attribute, using the very awesome wrappers generated by the Visual C++ #import directive.

The catch is that becausae a VARIANT is passed in, you can actually use other variant types that can be converted to a string. Here it goes.

#include <windows.h>
#include <iostream>
#include <iomanip>
#import "msxml6.dll"



    MSXML2::IXMLDOMDocument2Ptr the_document;

    _variant_t val(3);
    MSXML2::IXMLDOMElementPtr the_element = the_document->createElement(L
    the_document->documentElement = the_element;
"attribute", val);

// Output:
    // XML:
    // <doc attribute="3"/>
    std::wcout << L"XML:" << std::endl << the_document->xml;


Note that all interfaces will be released, and even the BSTR returned by the xml property is wrapper in a _bstr_t, so it's released when it goes out of scope. In fact, the extra pair of curly braces are there to make sure that everything gets release before the call to CoUninitialize.


Comments (0)

Skip to main content