Measurements of x-ray emission and absorption spectra of the constituents of MgB2 are presented. The results obtained are in good agreement with calculated x-ray spectra, with dipole matrix elements taken into account. The comparison of x-ray emission spectra of graphite, AlB2, and MgB2 in the binding energy scale supports the idea of charge transfer from σ to π bands, which creates holes at the top of the bonding σ bands and drives the high-Tc