We need the X-ray to give all its energy to some kind of detector. That energy will change something in the detector and, by measuring that change, we can determine the energy of the incident X-ray. X-rays interact strongly with electrons . You may know that lead is good at blocking X-rays. 82 electrons and lead is a metal, so the atoms are packed together pretty close.
Lead stops X-rays because it has a large electron density .
The probability associated with each of these kinds of scattering depends on the energy of the X-ray photon, the bound state the electron is in, and the scattering angle. So, when an X-ray stops in a detector, it has given all of its energy to one electron. That electron can rattle around in the detector and give energy to other electrons. In some materials, these electrons will have enough energy to be free of their host atoms. If an electric field is applied, these electrons can be collected and counted.