Microsoft XML 2.5 SDK


 

IXMLDOMNamedNodeMap::setNamedItem Method

[This is preliminary documentation and subject to change.]

Adds the supplied node to the collection.

Visual Basic Syntax

Set objXMLDOMNode = oIXMLDOMNamedNodeMap.setNamedItem(newItem)

C/C++ Syntax

HRESULT setNamedItem(

    IXMLDOMNode *newItem,

    IXMLDOMNode **nameItem);

Parameters

newItem

[in]
Attribute to be added to the collection.

nameItem

[out]
Attribute successfully added to the collection. If null, no object is created.

C/C++ Return Values

S_OK

Value returned if successful.

E_INVALIDARG

Value returned if newItem is null.

E_FAIL

Value returned if an error occurs.

Remarks

If an attribute already exists with the name in the supplied IXMLDOMNode 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.

C/C++ Example

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 done with it.