tags:

views:

283

answers:

2

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 I to work around by doing some insert/replace and deep copy children?

<xml> ...
    <oldTagName>
      ... sub-elements
    </oldTagName>
<more xml> ...

Should become

<xml> ...
    <newTagName>
      ... sub-elements
    </newTagName>
<more xml> ...
+1  A: 

According to Document Object Model you can't rename a node.

See: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247

renameNode is possible in DOM Level 3 but not in MSXML library

prostynick
A: 

Just for reference, I ended up with a replace function which is copying all children to a newly created node: (VB6 sample code)

Private Sub ReplaceNodeName(oDoc As DOMDocument, oElement As IXMLDOMElement, newName As String)
 Dim ohElement As IXMLDOMElement
 Dim sElement As IXMLDOMElement
 Dim oChild As IXMLDOMNode

 ' search the children '
 If Not oElement Is Nothing Then
  Set ohElement = oElement.parentNode
  Set sElement = oDoc.createElement(newName)

  For Each oChild In oElement.childNodes
   Call sElement.appendChild(oChild)
  Next

  Call ohElement.replaceChild(sElement, oElement)
 End If
End Sub
MicSim