title: Saving updated xml without beautifulsoup.prettify
body: <p>I have created this code to substitute some strings in xml file with other text. I used Beautifulsoup for this excersise and as instructed in the documentation i used soup.prettify in the end in order to save changed xml. However prettified xml is not working for me - i get errors when trying to import it back to the CMS.
  Is there any other way to save updated xml without changing xml structure and without re-writing the whole code. See my code for reference below. Thanks for advice!</p>
  <pre><code>import openpyxl
  import sys
  
  #searching for Part Numbers and descriptions in xml
  from bs4 import BeautifulSoup
  infile = open('name of my file.xml', &quot;r&quot;, encoding=&quot;utf8&quot;)
  contents = infile.read()
  infile.close()
  soup = BeautifulSoup(contents,'xml')
  all_Products = soup.find_all('Product')
  
  
  #gathering all Part Numbers from xml
  for i in all_Products:
      PN = i.find('Name')
      PN_Descr = i.find_all(AttributeID=&quot;PartNumberDescription&quot;)
      PN_Details = i.find_all(AttributeID=&quot;PartNumberDetails&quot;)
  
  
  for y in PN_Descr:
      PN_Descr_text = y.find(&quot;TranslatableText&quot;)
      try:
          string = PN_Descr_text.string
          PN_Descr_text.find(text=string).replace_with(&quot;New string&quot;)
       except AttributeError:
          print(&quot;Attribute error in: PN Description for: &quot;, PN)
          continue
  
  for z in PN_Details:
      PN_Details_text = z.find(&quot;TranslatableText&quot;)
      try:
          string = PN_Details_text.string
          PN_Details_text.find(text=string).replace_with(&quot;New string&quot;)
      except AttributeError:
          print(&quot;Attribute error in: PN Details for: &quot;, PN)
          continue
  
  xml = soup.prettify(&quot;utf-8&quot;)
  with open('name of my file.xml', &quot;wb&quot;) as file:
      file.write(xml)
  </code></pre>