Not sure if this is the right forum.

I am using this code to take data from the sql database and convert it into an xml file. However some of the row results from the mysql database have characters that cause errors with the xml file and it doesn't close the elements. For example if there is an apostrophe or euro sign. I thought the using writecdata would sort it but it still failed (also is the resulting xml meant to show the element as <![CDATA[ name ]]> ?)

Code:
<?php
include ("db.php");
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$sql_0 = "SELECT Name FROM properties";
$query0 = mysql_query($sql_0) or die("Cannot query the database.<br>" . mysql_error());

$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument('1.0', 'UTF-8');
$xml->setIndent(true);

$xml->startElement("Envelope");
$xml->startElement("Body");
$xml->startElement("add_adverts");

while($row = mysql_fetch_array($query0)){
$name = $row["Name"];
  $xml->startElement('advert');
  
  $xml->startElement("Name");
  $xml->writeCData ($name);
  $xml->endElement();
   
  $xml->endElement();

} 
$xml->endElement();
$xml->endElement();
$xml->endElement();
header('Content-type: application/xml; charset=utf-8');
header('Content-type: text/xml');
$xml->flush();
?>
Any help or pointers would be appreciated.

Thanks