public function toXML(&$oXMLout = null)
    {
        if ($oXMLout === null) {
            $oXMLout = new \XMLWriter();
            $oXMLout->openMemory();
            $oXMLout->setIndent(true);
            $oXMLout->setIndentString('    ');
        }

        $func = function ($obj, $basename = null) use (&$func, &$oXMLout) {
            $className = explode('\\', get_class($obj));

            $className = $basename ? $basename : lcfirst(end($className));

            $oXMLout->startElement($className);

            $vars = get_object_vars($obj);


            //move uid and updateid to the begining
            $vars = array('uid' => $vars['uid'], 'lastUpdatedUid' => $vars['lastUpdatedUid']) + $vars;

            foreach ($vars as $key => $value) {

                if ($value === null || strpos($key, '_') === 0) {
                    continue;
                }

                if (is_array($value)) {
                    $oXMLout->startElement($key);
                    foreach ($value as $subValue) {
                        if ($subValue instanceof EntityBase) {
                            $func($subValue, $subValue->getName());
                        }
                    }
                    $oXMLout->endElement();

                } elseif ($value instanceof EntityBase) {
                     $func($value, $key);

                } elseif (($key == 'uid' || $key == 'lastUpdatedUid') && $obj->getUidPlacement() == 'attribute') {
                    if ($value != '') {
                        $oXMLout->writeAttribute($key, $value);
                    }
                } else {
                    $oXMLout->writeElement($key, (string)$value);
                }
            }
            $oXMLout->endElement();

        };


        $func($this);

        $string = $oXMLout->outputMemory();

        //remove empty tags
        $regexps = array (
            '~<(\w+)\b[^\>]*>\s*</\\1>~',
            '~<\w+\s*/>~'
        );

        do
        {
            $temp = $string;
            $string = preg_replace ($regexps, '', $temp);
            $string = str_replace("    \n", '', $string);
        }
        while ($string != $temp);

        //remove extra spaces


        return $string;

    }