Adds the supplied node to the collection.
Set objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem)
HRESULT setNamedItem( IXMLDOMNode *newItem, IXMLDOMNode **nameItem);
If an attribute already exists with the name in the supplied XMLDOMNode object, the supplied object replaces the existing attribute. The attribute name appears in its IXMLDOMNode property.
You cannot add an existing attribute to an element until you first remove it from its previous element. Also, you cannot add a namespace-qualified attribute when it uses the same prefix as another attribute with a different namespaceURI.
If the newItem node type is not NODE_ATTRIBUTE, setNamedItem returns an error. For example, it is not possible to modify entities and notations, which are read-only.
HRESULT hr; IXMLDOMDocument *pIXMLDOMDocument = NULL; IXMLDOMNode *pIXMLDOMNode = NULL; IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL; BSTR bstrAttributeName = ::SysAllocString(_T("dateModified")); IXMLDOMAttribute *pIXMLDOMAttribute = NULL; IXMLDOMElement *pIXMLDOMElement = NULL; try { // Create an instance of DOMDocument and initialize pIXMLDOMDocument. // Load/create an XML fragment. hr = m_pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement); SUCCEEDED(hr) ? 0 : throw hr; if(pIXMLDOMElement) { hr = pIXMLDOMElement->get_attributes(&pIXMLDOMNamedNodeMap); if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap) { hr = m_pIXMLDOMDocument->createAttribute(bstrAttributeName, &pIXMLDOMAttribute); if(SUCCEEDED(hr) && pIXMLDOMAttribute) { hr = pIXMLDOMAttribute->put_nodeValue(_variant_t(_T("year 2000"))); hr = pIXMLDOMNamedNodeMap->setNamedItem(pIXMLDOMAttribute, &pIXMLDOMNode); if(SUCCEEDED(hr) && pIXMLDOMNode) { pIXMLDOMNode->Release(); pIXMLDOMNode = NULL; } pIXMLDOMAttribute->Release(); pIXMLDOMAttribute = NULL; } pIXMLDOMNamedNodeMap->Release(); pIXMLDOMNamedNodeMap = NULL; } pIXMLDOMElement->Release(); pIXMLDOMElement = NULL; } ::SysFreeString(bstrAttributeName); bstrAttributeName = NULL; } catch(...) { if(bstrAttributeName) ::SysFreeString(bstrAttributeName); if(pIXMLDOMElement) pIXMLDOMElement->Release(); if(pIXMLDOMNamedNodeMap) pIXMLDOMNamedNodeMap->Release(); if(pIXMLDOMAttribute) pIXMLDOMAttribute->Release(); if(pIXMLDOMNode) pIXMLDOMNode->Release(); DisplayErrorToUser(); } // Release pIXMLDOMDocument when finished with it.