You can put the 300D in the 126 -- you may have trouble getting some needed parts like driveshaft if no W126 diesel models were sold locally. You will have to get the parts via MB. Quite a bit of work, put do-able.
I suspect you will have serious diffuculty putting the 500 motor in the w123, though -- there might not be room for the exhaust manifolds, etc -- no V type engine was ever in that chassis by MB, so you would have to custom manufacture quite a bit of stuff. I'd measure carefully and see if it is theoretically possible before attempting it.
Several other threads address this (engines the chassis wasn't designed to hold being custom installed).
1972 220D ?? miles
1988 300E 200,012
1987 300D Turbo killed 9/25/07, 275,000 miles
1985 Volvo 740 GLE Turobodiesel 218,000
1972 280 SE 4.5 165, 000 - It runs!