title: Python XML validation against Schema lxml
body: <p>I am trying to validate an XML document against its XSD in Python.
  I am currently following <a href="https://emredjan.github.io/blog/2017/04/08/validating-xml/" rel="nofollow noreferrer">this</a> tutorial.</p>
  
  <p>I am able to read in my XML file without issue. However I need to provide an encoding type for the schema. Failing to do so results in a <code>'charmap' codec can't decode byte 0x81</code> error.</p>
  
  <pre><code>with open(os.path.join(settings.MEDIA_ROOT, xsd_filename), 'r', encoding='utf-8') as schema_file:
          schema_to_check = schema_file.read()
  </code></pre>
  
  <p>However, when I add the encoding etree is unable to parse the contents of the file? The error returned is <code>Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.</code></p>
  
  <pre><code>xmlschema_doc = etree.parse(StringIO(schema_to_check))
  </code></pre>
  
  <p>I'm not sure I know what's going on. From the lxml site I get the impression that I should leave the encoding alone but I'm not sure why the tutorial works and mine doesn't?</p>
  
  <p>My version of Python is 3.6.4.</p>
  
  <p>Thanks in advance.</p>