Change NodeName of an XML tag element using MSXML

后端 未结 4 1175
滥情空心
滥情空心 2021-01-19 08:03

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

4条回答
  •  春和景丽
    2021-01-19 08:13

    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
    

提交回复
热议问题