Even though xmlns looks like attribute node to the top element, and even though you can access this using topElement.Attributes.getNamedItem("xmlns") or topElement.getAttributeNode("xmlns"), still removeAttribute and objXMLDoc.namespace.remove will not work - because of the fact that namespace is really part of the element name, which is read only. The only option we can think of is to re-create the document - to do this, you can use DOM or apply XSLT.
Here is an example that we wrote for you. It gets rid of the default namespace and adds a child element node. We use XSLT for this.
Source XML Document:
<?xml version="1.0" encoding="utf-8"?>
<empDetails xmlns="33BFA363-E5F3-43da-B895-302C108ADCAC">
<emp id="1" firstName="John" lastName="smith"/>
<emp id="2" firstName="Mark" lastName="Robinson"/>
<emp id="3" firstName="Dave" lastName="Schur"/>
</empDetails>
XSLT Stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="33BFA363-E5F3-43da-B895-302C108ADCAC"
exclude-result-prefixes="x">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<empDetails>
<Meta name="schema" content="33BFA363-E5F3-43da-B895-302C108ADCAC" />
<xsl:apply-templates />
</empDetails>
</xsl:template>
<xsl:template match="x:emp">
<emp>
<xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>
<xsl:attribute name="firstName"><xsl:value-of select="@firstName" /></xsl:attribute>
<xsl:attribute name="lastName"><xsl:value-of select="@lastName" /></xsl:attribute>
</emp>
</xsl:template>
</xsl:stylesheet>
Visual Basic code to apply the stylesheet:
Dim objXML As New MSXML2.DOMDocument40
Dim objXSL As New MSXML2.DOMDocument40
objXML.async = False
objXML.validateOnParse = False
objXML.resolveExternals = False
If objXML.Load("c:\emp.xml") Then
objXSL.async = False
objXSL.validateOnParse = False
objXSL.resolveExternals = False
If objXSL.Load("c:\emp.xsl") Then
'MsgBox objXML.transformNode(objXSL)
objXML.transformNodeToObject objXSL, objXML
MsgBox objXML.xml
Else
With objXSL.parseError
MsgBox "Error: " & .errorCode & _
": " & .reason & vbNewLine & .srcText
End With
End If
Else
With objXML.parseError
MsgBox "Error: " & .errorCode & _
": " & .reason & vbNewLine & .srcText
End With
End If
|