IXMLDOMDocument::createElement

The createElement method creates an element node using the specified name.

HRESULT createElement(
BSTR bstrTagName, 
IXMLDOMElement** ppElement
);

Arguments

bstrTagName

[in] BSTR specifying the name for the new element node. The name is case-sensitive. This name is subsequently available as the element node's get_nodeName method.

ppElement

[out] Pointer to a pointer to an IXMLDOMElement interface for the new element. This method calls AddRef internally. To avoid memory leaks, you must call Release when you are finished using the interface.

Return Value

If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.

Remarks

Creating an element with this method is the same as using createNode where the type parameter value is NODE_ELEMENT and no namespace is specified.

To add the new object, you must explicitly call one of the node insert methods, insertBefore, replaceChild, or appendChild.

The get_nodeType method returns the value NODE_ELEMENT.

Example

The following example creates an element called "media" and appends it to the root element.

#include “wmsserver.h”
#include <atlbase.h> // Includes CComVariant and CComBSTR.

// Declare variables.
IWMSServer*           pServer;
IXMLDOMDocument*      pPlaylist;
IXMLDOMElement*       pXMLElement;
IXMLDOMElement*       pXMLMediaElement;
IXMLDOMNode*          pXMLNode;

HRESULT               hr;
CComBSTR              bstrName;
CComVariant           varValue;
VARIANT_BOOL          bIsSuccessful;

// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer, NULL, CLSCTX_ALL, 
       IID_IWMSServer, (void**)&pServer);
if (FAILED(hr)) goto EXIT;

// Create the playlist object.
hr = pServer->CreatePlaylist(&pPlaylist);

// Load a sample playlist file.
varValue = "c:\\wmpub\\wmroot\\simple.wsx";
hr = pPlaylist->load(varValue, &bIsSuccessful);
if (FAILED(hr)) goto EXIT;

if (bIsSuccessful)
{
    // Retrieve a pointer to the IXMLDOMElement interface.
    hr = pPlaylist->get_documentElement(&pXMLElement);
    if (FAILED(hr)) goto EXIT;

    // Create a media element.
    bstrName = "media";
    hr = pPlaylist->createElement(bstrName, &pXMLMediaElement);
    if (FAILED(hr)) goto EXIT;

    // Add the media element as a child node to the root element.
    hr = pXMLElement->appendChild(pXMLMediaElement, &pXMLNode);
    if (FAILED(hr)) goto EXIT;
}

EXIT:
    // TODO: Release temporary COM objects and uninitialize COM.

Requirements

Header: wmsserver.h.

Library: WMSServerTypeLib.dll.

Platform: Windows Server 2003 family, Windows Server 2008 family.

See Also

Reference

IXMLDOMDocument Interface

IXMLDOMElement Interface

Concepts

XML DOM Methods (C++)