By Martin Petty and Mai Nguyen

HANOI (Reuters) - Visiting Chinese President Xi Jinping and Vietnam's top leaders agreed on Thursday to manage disputes over the South China Sea amid strained ties made more uncertain by an upcoming reshuffle of Vietnam's ruling Communist Party.

Any cracks in relations were covered over as Xi met party heavyweights, some of whom have been wooed by the United States since a breakdown in Sino-Vietnamese ties last year following China's parking of an oil rig in contested waters.

Xi told Vietnamese Prime Minister Nguyen Tan Dung that the two sides "needed to manage disagreements well, using consultations to maintain (and) preserve stability on the seas," a Vietnamese Foreign Ministry statement said.

Xi's visit was timely, aimed at rebuilding relations amid some uncertainty over what kind of leader will emerge from January's five-yearly congress of a party traditionally close to Beijing, but now receiving unprecedented Western engagement.

Twelve bilateral agreements in all were signed, including cultural or "friendship" exchanges and several financing deals involving Chinese banks, most of which disclosed no details or values.

The agreements were made prior to Xi's meeting with Dung, whose government has been courting foreign investors and negotiating numerous trade pacts covering markets in Europe, Asia and North America with economies worth a combined $46 trillion.

Among those is the U.S.-led Trans Pacific Partnership, which Beijing is not part of.

Dung was the first of Vietnam's leaders to publicly criticize China over the rig incident, which gave rise to the worst breakdown in bilateral relations for three decades.

He stressed to Xi that the two sides should "control dissent" and "sincerely and frankly exchange and study demilitarization in the East Sea (South China Sea), ensuring security and safety of navigation and aviation," according to the statement.

China's rapid reclamation work near the contested Spratly Islands has fueled resentment and put Vietnam's leaders in a tricky spot, as has recent sparring between Beijing and Washington over freedom of navigation.

Story continues

Small anti-China demonstrations have been held this week, including a brief protest on Thursday amid large police presence outside China's embassy in Hanoi.

Vietnam has been diversifying its ties and although China is not among its top investors, it is its largest trading partner at $60 billion a year and biggest source of its imports, making for a dependence that remains a contentious domestic issue.

(Additional reporting by Ben Blanchard in Beijing)