Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert a node into an XML DOM document before the selected node. The new node is created fine as I have output it to a mesage box, but it always crashes on the 'insertBefore' line. Please tell me what I am doing wrong - I have looked at so many examples and can't see why my code is different.

MSXML2::IXMLDOMDocumentPtr wspXMLDocPtr
MSXML2::IXMLDOMNodePtr selectedNodePtr;//this is set to a valid node - I have checked

MSXML2::IXMLDOMNodePtr newNode = m_wspXMLDocPtr->createNode(_variant_t((short)       MSXML2::NODE_ELEMENT),"SEQUENCE", "");
			
m_wspXMLDocPtr->insertBefore(newNode, _variant_t(selectedNodePtr.GetInterfacePtr()));
Posted

1 solution

You cannot call insertBefore on a Document for inserting a NODE_ELEMENT, see Calling insertBefore on Documents at MSDN[^].

You probably have to find the actual parent of the selectedNode and call insertBefore on it.
 
Share this answer
 
v2
Comments
Jackie Lloyd 8-Nov-11 6:39am    
Thankyou -
I have also tried changing the last line to:

selectedNodePtr->insertBefore(newNode, _variant_t(selectedNodePtr.GetInterfacePtr()));

but it still doesn't work
CPallini 8-Nov-11 7:00am    
Use the parentNode property of selectedNodePtr for retrieving its parent and then call insertBefore on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900