public function toXML()
    {

        $oXMLout = new \XMLWriter();
        $oXMLout->openMemory();
        $oXMLout->setIndent(true);
        $oXMLout->setIndentString('    ');
        $className = explode('\\', get_class($this->entity));

        $className = lcfirst(end($className));

        $nodeName = lcfirst(($this->taskType) . ucfirst($className));

        $oXMLout->startElement($nodeName);

        if($this->entity instanceof Invoice && $this->option && $this->option instanceof InvoiceInstruction){
            $oXMLout->writeAttribute("emailToContact", $this->option->emailToContact);
            if($this->option->templateUid > 0){
                $oXMLout->writeElement('templateUid', $this->option->templateUid);
            }
            if($this->option->emailMessage){
                $oXMLout->writeRaw( "\n".$this->option->emailMessage->toXML());
            }
        }


        $oXMLout->writeRaw("\n".$this->entity->toXML());

        $oXMLout->endElement();

        $string = $oXMLout->outputMemory();

        return $string;

    }