Serializes a :class:`Entity` instance to an XML string.

        The default character encoding is ``utf-8`` and can be set via the
        `encoding` parameter.

        If `encoding` is ``None``, a unicode string is returned. That is, a
        :class:`unicode` in Python 2 and a :class:`str` in Python 3.

        Notes:
            If you need to print a byte string in Python 3, you will need to
            decode it using using the :class:`str`.

        Args:
            include_namespaces (bool): whether to include xmlns and
                xsi:schemaLocation attributes on the root element. Set to true by
                default.
            namespace_dict (dict): mapping of additional XML namespaces to
                prefixes
            pretty (bool): whether to produce readable (``True``) or compact
                (``False``) output. Defaults to ``True``.
            encoding: The output character encoding. Default character encoding
                is ``utf-8``.

        Returns:
            A byte string containing the XML representation for this :class:`Entity`
            instance if an encoding was provided. If encoding is set to
            ``None`` a unicode string containing the XML representation will be
            returned.

def to_xml(self, include_namespaces=True, namespace_dict=None,
               pretty=True, encoding="utf-8"):
        """Serializes a :class:`Entity` instance to an XML string.

        The default character encoding is ``utf-8`` and can be set via the
        `encoding` parameter.

        If `encoding` is ``None``, a unicode string is returned. That is, a
        :class:`unicode` in Python 2 and a :class:`str` in Python 3.

        Notes:
            If you need to print a byte string in Python 3, you will need to
            decode it using using the :class:`str`.

        Args:
            include_namespaces (bool): whether to include xmlns and
                xsi:schemaLocation attributes on the root element. Set to true by
                default.
            namespace_dict (dict): mapping of additional XML namespaces to
                prefixes
            pretty (bool): whether to produce readable (``True``) or compact
                (``False``) output. Defaults to ``True``.
            encoding: The output character encoding. Default character encoding
                is ``utf-8``.

        Returns:
            A byte string containing the XML representation for this :class:`Entity`
            instance if an encoding was provided. If encoding is set to
            ``None`` a unicode string containing the XML representation will be
            returned.

        """
        namespace_def = ""

        ns_collector = NamespaceCollector()
        gds_obj = self.to_obj(ns_info=ns_collector if include_namespaces else None)

        if include_namespaces:
            ns_collector.finalize(namespace_dict)
            delim = "\n\t" if pretty else " "
            namespace_def = ns_collector.get_xmlns_string(delim) + delim + \
                ns_collector.get_schema_location_string(delim)

        with save_encoding(encoding):
            sio = six.StringIO()
            gds_obj.export(
                sio.write,
                0,
                namespacedef_=namespace_def,
                pretty_print=pretty
            )

        # Ensure that the StringIO buffer is unicode
        s = six.text_type(sio.getvalue())

        if encoding:
            return s.encode(encoding)

        return s