Используйте типичную структуру try ... catch ... finally и, основываясь на конкретном типе исключения и сообщении, решите, хотите ли вы записать в xml или нет.
try
{
SqlConnection connection = new SqlConnection(DB("Your DB Name"));
connection.Open();
}
catch (Exception ex)
{
// check the exception message here, if it's telling you that the db is not available. then
//write to xml file.
WriteToXml();
}
finally
{
connection.Close();
}