A solenoid valve comprises a housing 2 having an axial bore 7 which is in flow connection with at least an inlet port and an outlet port 3, 5, a valve element 8 which is moveable to and fro in the axial direction of the bore 7, an electrical coil 13 for generating a magnetic field for moving the valve element 8 between a first end position, in which it lies sealing against a seat 9′ in order to disconnect said inlet and outlet port 3, 5 from each other, and a second end position, in which it lies at a distance from said seat 9′ in order to create a flow opening for connecting said inlet and outlet port 3, 5 with each other, and a position sensor 17 for detecting axial positions of the valve element 8 in the axial direction of the bore 7. A control unit C is provided for determining stroke, velocities and/or accelerations of the valve element 8 in the axial direction of the bore 7 as a function of the detected axial positions during movements of the valve element 8 between its first and second end positions.