I am trying to parse a XML file, modify some values, and write the file back using the python code below. When writing the file back, it changes the namespace declaration location from assemblyBinding
element to root
(configuration) element. Due to this I am having other issues when that XML file is consumed. Is it possible to maintain the location of namespace declaration?
import xml.etree.ElementTree as ET
ET.register_namespace('', "urn:schemas-microsoft-com:asm.v1")
tree = ET.parse('abc.xml')
root = tree.getroot()
..
tree.write('abc.xml')
Original Configuration
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity culture="neutral"/>
</assemblyBinding>
</runtime>
</configuration>
Modified Configuration
<configuration xmlns="urn:schemas-microsoft-com:asm.v1">
<runtime>
<assemblyBinding>
<assemblyIdentity culture="neutral"/>
</assemblyBinding>
</runtime>
</configuration>
I am using Python 3.7 Thank you.