Something like this should work:
xmlSaveCtxtPtr saveCtxt = xmlSaveToFilename(filename, NULL, XML_SAVE_NO_DECL);
if (saveCtxt == NULL) {
// Error handling
}
if (xmlSaveDoc(saveCtxt, doc) < 0) {
// Error handling
}
xmlSaveClose(saveCtxt);
The documentation of the xmlsave module can be found here.