China's Xi affirms friendship with N.Korean leader, gets denuclearisation pledge

* N.Korean leader visited Beijing from Sunday-Wednesday

* Chinese state media publishes photo of Xi, Kim

* Visit was Kim's first known trip outside North Korea

* Xi accepts invitation to visit N.Korea - Yonhap (Recasts, adds details, quotes, previous SHANGHAI/SEOUL)

By Ben Blanchard and Joyce Lee

BEIJING/SEOUL, March 28 (Reuters) - China said on Wednesday it won a pledge from North Korean leader Kim Jong Un to denuclearise the Korean peninsula during a meeting with President Xi Jinping, who pledged in return that China would uphold its friendship with its isolated neighbour.

After two days of speculation, China announced on Wednesday that Kim had visited Beijing and met Xi during what the official Xinhua news agency called an unofficial visit from Sunday to Wednesday.

The trip was Kim's first known journey abroad since he assumed power in 2011 and is believed by analysts to serve as preparation for upcoming summits with South Korea and the United States.

Beijing has traditionally been the closest ally of secretive North Korea, but ties have been frayed by North Korea's pursuit of nuclear weapons and China's backing of tough U.N. sanctions in response.

Xinhua cited Kim as telling Xi that the situation on the Korean peninsula is starting to improve because North Korea has taken the initiative to ease tensions and put forward proposals for peace talks.

"It is our consistent stand to be committed to denuclearisation on the peninsula, in accordance with the will of late President Kim Il Sung and late General Secretary Kim Jong Il," Kim Jong Un said, according to Xinhua.

North Korea is willing to talk with the United States and hold a summit between the two countries, he said.

"The issue of denuclearisation of the Korean Peninsula can be resolved, if South Korea and the United States respond to our efforts with goodwill, create an atmosphere of peace and stability while taking progressive and synchronous measures for the realisation of peace," Kim said.

Xi told Kim in return that both sides had stated repeatedly that their traditional friendship should be passed on and developed better. "This is a strategic choice and the only right choice both sides have made based on history and reality, the international and regional structure and the general situation of China-North Korea ties. This should not and will not change because of any single event at a particular time," Xi said.

Xinhua published a photograph of Kim and Xi shaking hands in front of the flags of the two nations.

Speculation about a possible visit by Kim to Beijing was rife earlier this week after a train similar to the one used by Kim's father was seen in the Chinese capital, along with heavy security and a large motorcade.