The electronic structure of the ͑Ga,Mn͒As, ͑Ga,Mn͒N, and ͑Ga,Gd͒N, diluted magnetic semiconductors ͑DMSs͒ were investigated theoretically from first principles, using the fully relativistic Dirac linear muffin-tin orbital band-structure method. The electronic structure is obtained with the local spin-density approximation ͑LSDA͒, as well as the LSDA+ U method. The x-ray magnetic circular and linear dichroism ͑XMCD and XMLD͒ spectra at the Mn, As, Ga, and N K and Gd, Mn, As, and Ga L 2,3 edges were investigated theoretically from first principles. The origin of the XMCD spectra in these compounds was examined. The effect of interstitial Mn atoms was found to be crucial for the x-ray magnetic dichroism at the Mn and As K and L 2,3 edges in the ͑Ga,Mn͒As DMS. The influence of the exchange splitting and spin-orbit coupling strength on each of the constituent atoms were furthermore analyzed. The orientation dependence of the XMCD and XMLD at the Mn L 2,3 edges in the ͑Ga,Mn͒As DMS was investigated by calculating the XMCD and XMLD spectra for the ͗001͘ and ͗110͘ magnetization axis. We found a quite small anisotropy in the XMCD and a giant anisotropy in the XMLD at the Mn L 2,3 edges. The exchange splitting of the initial Mn 2p core level was found to be responsible for huge magnetocrystalline anisotropy of the XMLD at the Mn L 2,3 edges. The calculated results are compared with available experimental data.