You were lucky that 1 got output, if you use
then nothing will be written to the file,
The problem is the tex--xet bidirectional model that xetex inherited from etex. right to left directionality isn't built in to the system at a low level the way it is in the model luatex inherited from omega, it is essentially a simple pass that reverses nodes (just) in horizontal boxes.
So in vertical mode (the file open and the first
\write in your example, it's OK but the line of the paragraph with the later writes and the file close, the nodes are reversed, and so the file close happens first, which means the
\write are writing to a closed file so just appear on the terminal and log.
One possible scheme would be