I'm trying to export some data from the database to XML file and then send the file to the browser (downloadable) , i can now download the file , but some data are missing !
here is the the part of the code where the problem exists:
<?php 
include('session.php');
if (isset($_POST['exportxml'])) {
$filename = 'Data_Backup_' . date('m-d-Y_his') . '.xml';
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$xml = new XMLWriter();
header('Content-Disposition: attachment;filename=' . $filename);
header('Content-Type: text/xml');
$xml->openURI("php://output");
$xml->setIndent(true);
$xml->startElement('data');
if (isset($_POST['exportaccounts'])) {
    $query = "SELECT * from users";
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $xml->startElement("useraccount");
            $xml->writeElement("id", $row['id']);
            $xml->writeElement("username", $row['username']);
            $xml->writeElement("email", $row['email']);
            $xml->writeElement("state", $row['state']);
            $xml->endElement();
        }
    }
}
if (isset($_POST['exporttimzezone'])) {
    $query7 = "SELECT * from options where optionname = 'timezone'";
    $result7 = mysqli_query($conn, $query7);
    if (mysqli_num_rows($result7) > 0) {
        while ($row7 = mysqli_fetch_assoc($result7)) {
            $xml->startElement("timezone");
            $xml->writeElement("id", $row7['id']);
            $xml->writeElement("optionname", $row7['optionname']);
            $xml->writeElement("value", $row7['value']);
            $xml->writeElement("state", $row7['state']);
            $xml->endElement();
        }
    }
}
$xml->startElement("version");
$xml->writeElement("id", "1.0");
$xml->endElement();
$xml->endElement();
echo $domtree->saveXML();
exit;
$xml->flush();
?>
I don't know what is the cause of missing data problem because i never dealt with XML functions in php before .
P.S: all the connections to the database are successful and all the data exist and fetched without any problem from the tables
Regards
 
    