我正在尝试解析XML文件,修改一些值,然后使用下面的python代码将文件写回。回写文件时,它将名称空间声明位置从assemblyBinding
元素更改为root
(配置)元素。因此,使用该XML文件时我遇到其他问题。可以维护名称空间声明的位置吗?
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')
原始配置
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity culture="neutral"/>
</assemblyBinding>
</runtime>
</configuration>
修改后的配置
<configuration xmlns="urn:schemas-microsoft-com:asm.v1">
<runtime>
<assemblyBinding>
<assemblyIdentity culture="neutral"/>
</assemblyBinding>
</runtime>
</configuration>
我正在使用Python 3.7 谢谢。