I\'d like to change the tag name of a MSXML XMLDOMElement, but unfortunately the nodeName property is read-only. Is there any straightforward way to do it, or have
This is my c++ version, maybe you will understand it easier:
#define SAFERELEASE(comobj) \
if (comobj) \
{ \
comobj->Release(); \
comobj = NULL; \
}
void RenameElement(IXMLDOMElement * pElement, LPWSTR newName)
{
if (NULL == pElement)
return;
IXMLDOMDocument * pParentDoc = NULL;
pElement->get_ownerDocument(&pParentDoc);
IXMLDOMElement * pNewElement = NULL;
pParentDoc->createElement(newName, &pNewElement);
VARIANT_BOOL bHasNodes;
pElement->hasChildNodes(&bHasNodes);
while (bHasNodes)
{
IXMLDOMNode * pFirstChild = NULL;
pElement->get_firstChild(&pFirstChild);
pNewElement->appendChild(pFirstChild, NULL);
pElement->hasChildNodes(&bHasNodes);
SAFERELEASE(pFirstChild);
}
IXMLDOMNamedNodeMap * pAttrMap = NULL;
IXMLDOMNamedNodeMap * pAttrMapNew = NULL;
pElement->get_attributes(&pAttrMap);
pNewElement->get_attributes(&pAttrMapNew);
long nAttrLength = 0;
pAttrMap->get_length(&nAttrLength);
for (int n = 0; n < nAttrLength; n++)
{
IXMLDOMNode * pListItem = NULL;
pAttrMap->get_item(n, &pListItem);
BSTR wsAttrName = NULL;
pListItem->get_nodeName(&wsAttrName);
BSTR wsAttrValue = NULL;
pListItem->get_text(&wsAttrValue);
IXMLDOMAttribute * pAttribute = NULL;
pParentDoc->createAttribute((BSTR)wsAttrName, &pAttribute);
pAttribute->put_value(CComVariant((BSTR)wsAttrValue));
pAttrMapNew->setNamedItem(pAttribute, NULL);
SAFERELEASE(pAttribute);
SysFreeString(wsAttrValue);
SysFreeString(wsAttrName);
SAFERELEASE(pListItem);
}
IXMLDOMNode * pParent = NULL;
pElement->get_parentNode(&pParent);
pParent->replaceChild(pNewElement, pElement, NULL);
SAFERELEASE(pAttrMapNew);
SAFERELEASE(pAttrMap);
SAFERELEASE(pParent);
SAFERELEASE(pNewElement);
SAFERELEASE(pParentDoc);
}