title: Don't get a usable XML string with .tostring when using xml.etree.ElementTree in python
body: <p>I've tried a lot, but I haven't found a working solution to my problem, so I hope you can help me:</p>
  
  <p>I am about to write a python module, which sends an XML request to a DNS server, receives an XML as response and should process this response. However, I am already failing in sending the request.</p>
  
  <p>I have an XML base structure which I want to fill with different elements depending on the action to be performed on the DNS.
  For this purpose I read in an XML string with <a href="https://python.readthedocs.io/en/stable/library/xml.etree.elementtree.html#xml.etree.ElementTree.fromstring" rel="nofollow noreferrer">.fromstring</a>, edit the xml object and want to send it back to the server with <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring" rel="nofollow noreferrer">.tostring</a>. The problem is that <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring" rel="nofollow noreferrer">.tostring</a> does not return a usable xml string. The following example shows what I mean:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import requests   
  
  headers = {'HeaderSOAP': 'SOAPAction:urn:QIPServices#getEntry'}
  body = """&lt;soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
              instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
              xmlns:urn="urn:QIPServices"&gt;
              &lt;soapenv:Header/&gt;
              &lt;soapenv:Body&gt;
                 &lt;urn:getEntry soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
                  &lt;login xsi:type="xsd:string"&gt;USERNAME&lt;/login&gt;
                  &lt;password xsi:type="xsd:string"&gt;PASSWD&lt;/password&gt;
                  &lt;sharedsecret xsi:type="xsd:string"&gt;SECRET&lt;/sharedsecret&gt;
                  &lt;VPN xsi:type="xsd:string"&gt;VPN&lt;/VPN&gt;
                  &lt;IPoderName xsi:type="xsd:string"&gt;IPorFQDN&lt;/IPoderName&gt;
                 &lt;/urn:getEntry&gt;
              &lt;/soapenv:Body&gt;
           &lt;/soapenv:Envelope&gt;
          """
  root = ET.fromstring(body)
  ThatsTheProblem = ET.tostring(root, encoding='utf-8')
  print(ThatsTheProblem)
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>b'&lt;ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"   
  xmlns:ns1="urn:QIPServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
  instance"&gt;\n                &lt;ns0:Header /&gt;\n                &lt;ns0:Body&gt;\n                   
  &lt;ns1:getEntry 
   ns0:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;\n                    
  &lt;login xsi:type="xsd:string"&gt;USERNAME&lt;/login&gt;\n                    &lt;password 
  xsi:type="xsd:string"&gt;PASSWD&lt;/password&gt;\n                    &lt;sharedsecret 
  xsi:type="xsd:string"&gt;SECRET&lt;/sharedsecret&gt;\n                    &lt;VPN 
  xsi:type="xsd:string"&gt;VPN&lt;/VPN&gt;\n                    &lt;IPoderName 
  xsi:type="xsd:string"&gt;IPorFQDN&lt;/IPoderName&gt;\n                   
  &lt;/ns1:getEntry&gt;\n                &lt;/ns0:Body&gt;\n             &lt;/ns0:Envelope&gt;'
  </code></pre>
  
  <p>Without having changed anything, the import and output not only changed the complete formatting, there are also whitespaces everywhere in the file. When I send this XML to the server using
  <code>response = requests.post(url,data=ThatsTheProblem, headers=headers)</code><br>
  I get the following answer:</p>
  
  <blockquote>
    <p>Application failed during request deserialization: Unresolved prefix \'xsd\' for attribute value \'xsd:string\'\n</p>
  </blockquote>
  
  <p>Which I attribute to the problem I described at the beginning. </p>
  
  <p>If anyone has a solution to this problem I would be very grateful.</p>
  
  <p>Thanks and have a nice day.</p>