What code do i use so that when the exe program is terminated, it flushes its buffer?

The program receives information and then displayes it in an XML file. When the program is terminated, it does not display the information it has stored, how do i make it so that it takes all the information it has stored and displays it in the XML file?

Thanks in advance.