We propose and discuss a method to analyze the structures of regular magnetic fields in external spiral galaxies with allowance for a multi-layer distribution of magnetic field and thermal electron density in the source of polarized radio emission. Our method allows for both horizontal and vertical components of the regular magnetic field. This approach we applied to the analysis of polarization observations of M 51 at the wavelengths λλ2.8, 6.2, 18.0 and 20.5cm smoothed to a resolution of =~3.5kpc. We fitted the observed azimuthal distributions of the polarization angle within rings of 3kpc width for the radial range between 3 and 15kpc in M 51. We found a magneto-ionic halo in M 51 with a radial extent of about 10kpc. The regular magnetic fields in the disk and the halo have different structures. The regular magnetic field in the halo is axisymmetric and horizontal. Its field lines are spirals pointing inwards and generally opposite to those in the disk. The azimuthal structure of the magnetic field in the disk is fairly complicated; it is neither axisymmetric nor bisymmetric but can be satisfactorily represented by a superposition of these two basic harmonics with about equal weights. Magnetic lines of the regular field in the disk are spirals generally directed outwards. We compare the magnetic field parameters deduced from our analysis with those implied by the observed total and polarized intensities and equipartition arguments. Using also data on the thermal radio emission from the M 51 disk, we show that all these results can be combined into a coherent picture of the global magnetic pattern in M 51 which includes a thermal disk and an extended gaseous halo. The regular magnetic field strength averaged in 3kpc wide rings is about 5-10μG in the disk and reaches about 3μG in the radial range 3-6kpc in the halo. With the available resolution, the vertical component of the magnetic field is negligible inside the galactocentric radius of 12 kpc. The general features of the magnetic patterns revealed in the disk and the halo (e.g., a reversal between the disk and the halo and the azimuthal structures of the field in these two regions) seem to be in agreement with predictions of dynamo theory, but detailed modelling of a dynamo in M 51 is required to reach definite conclusions.