During Sui Dynasty
Fang Xuanling was born in 579, shortly before the founding of Sui Dynasty in 581, during Sui's predecessor state Northern Zhou. His great‐grandfather Fang Yi was a general, official, and hereditary count during Northern Wei, and his grandfather Fang Xiong was also an official. His father Fang Yanqian was a county magistrate during Sui.
Fang Xuanling was said to be intelligent and well‐learned in his youth, and particularly skillful at . It was said that once, when he accompanied his father to the capital Chang'an, the state was peaceful, and the popular sentiment was that Sui would last a long time. However, Fang Xuanling secretly opined to his father:
Fang Yanqian was surprised by his son's opinion, which, however, eventually turned out to be prophetic. When he was 17, he was successful at the imperial examination, and he became a military officer. He particularly impressed the deputy minister of civil service, Gao Xiaoji . However, it appeared that he did not serve long, as his father became ill, and the illness lasted 10 years, during which Fang Xuanling attended to him earnestly. After his father's death, he fasted for five days. He later became the magistrate of Xicheng County .
In 617, when the general rebelled against the rule of Emperor Wen's son , one of Li Yuan's major generals was his son . Fang Xuanling went to Li Shimin's camp and offered his services. It was said that as soon as Li Shimin met Fang, they became like old friends, and Li Shimin invited him to serve on staff. Fang served Li Shimin faithfully, and wherever Li Shimin campaigned, while his staff members would collect treasures, Fang spent the time interviewing the people of the area and retained the capable people to add to Li Shimin's staff.
In 618, after news arrived at Chang'an that Emperor Yang had been killed in a coup at Jiangdu , led by the general Yuwen Huaji, Li Yuan had Emperor Gong yield the throne to him, establishing Tang Dynasty as its Emperor Gaozu. He created Li Shimin the Prince of Qin. Fang continued to serve on Li Shimin's staff.
During Emperor Gaozu's reign
In 621, when Li Shimin defeated Tang's major enemy Wang Shichong the Emperor of Zheng and captured the Zheng capital Luoyang , it was said that he sent Fang to the offices of Sui's legislative and examination bureaus of government to try to preserve Sui archives, but Fang's mission turned out to be unsuccessful as the archives had already been destroyed by Wang.
Later in 621, when Emperor Gaozu, awarding Li Shimin for his great accomplishments, bestowed on him the unprecedented title of "Grand General of Heavenly Tactics" , Li Shimin built a mansion where he housed those staff members with the best literary talent, supplying them with the best food and supplies and had them conduct research and writing. Fang was part of this establishment, along with, among others, the fellow future Du Ruhui and Xu Jingzong. When Du was subsequently commissioned as a prefectural secretary general, Fang told Li Shimin that Du was an uncommon talent that he should do everything he could to retain, and Li Shimin thus persuaded Emperor Gaozu to allow Du to remain on his staff. It was said that Fang was capable in planning and strategizing, but not decisive in his decisions, while Du was capable in making quick and correct decisions, and they divided their strategical responsibilities while on Li Shimin's staff in that manner.
By 626, Li Shimin was locked in an intense rivalry with his older brother, Li Jiancheng the Crown Prince, and Fang and Du often suggested that he act first against Li Jiancheng. As both Li Jiancheng and another brother who supported Li Jiancheng, Li Yuanji the Prince of Qi, feared Fang's and Du's strategic capabilities, they falsely accused both Fang and Du and had them demoted out of Li Shimin's staff. In summer 626, when Li Shimin decided to act against Li Jiancheng and Li Yuanji, however, he summoned Fang and Du to his mansion. Fang and Du, initially fearing Emperor Gaozu's orders forbidding them to serve Li Shimin, declined. In anger, Li Shimin sent the general Yuchi Jingde to summon Fang and Du, with directions if they declined again, to kill them. Yuchi, however, was able to persuade them that Li Shimin was in fact intending on acting against Li Jiancheng and Li Yuanji, and so Fang and Du put on disguises as Taoist monks and were able to get to Li Shimin's mansions, where they assisted Li Shimin in planning the ambush against Li Jiancheng and Li Yuanji. Li Shimin subsequently ambushed his brothers as and killed them, and then effectively forced Emperor Gaozu to create him crown prince. Once he was created crown prince, Fang and Du were restored to honored positions on his staff, and soon, Fang was made the ''Zhongshu Ling'' — the head of the legislative bureau and a post considered one for a chancellor. Two months later, Emperor Gaozu yielded the throne to Li Shimin .
During Emperor Taizong's reign
Later in 626, when Eastern Tujue's Jiali Khan Ashina Duobi launched a major incursion into Tang territory, reaching all the way to Chang'an, Fang Xuanling, along with Gao Shilian, attended to Emperor Taizong as he personally met Ashina Duobi to promise additional tributes, to induce Ashina Duobi to withdraw.
Late in 626, when Emperor Taizong personally ranked the contributions of the generals and officials in order to grant them fiefs, Emperor Taizong ranked five of them — Fang, Zhangsun Wuji, Du Ruhui, Yuchi Jingde, and Hou Junji to be contributors of the highest grade, and Fang was created the Duke of Han. When Emperor Gaozu's cousin Li Shentong the Prince of Huai'an, himself a key general, objected to being ranked below Fang and Du, stating that Fang and Du, in particular, were only bureaucrats, Emperor Taizong pointed out that it was with their strategies that he was able to become emperor, Li Shentong relented, which led to the other objectors to also cease their objections. Later in 627, at Emperor Taizong's direction, Fang carried out a major simplification of the central government, reducing the number of officials at the central government to 634.
In 629, Fang, along with Du, became ''Puye'' , a head of the important executive bureau of the government . It was said that at this time that the Fang and Du became known as the model for chancellors. It was also said that Fang, even though he had reached the pinnacle of officials' careers, was still very humble before the emperor, and whenever he received a rebuke, he would act as if disaster was coming. He also became responsible for organizing the imperial archives and the writing of official histories. Later that year, the assistant imperial censor Quan Wanji accused Fang and Wang Gui of being partial in their selections of officials, and Emperor Taizong initially ordered Hou to investigate. However, at the urging of another chancellor, Wei Zheng, who pointed out that Fang and Wang were trusted officials given important responsibilities, and it was inappropriate to pick on details of what they did, Emperor Taizong stopped the investigation.
In 630, when Emperor Taizong began to have his crown prince Li Chengqian formally rule on a number of matters of state, he commissioned Fang and Li Gang to sit with the Crown Prince and assist him on making the decisions. Also in 630, his title was changed to Duke of Wei.
In 636, for reasons unknown, Fang was briefly removed from his post and returned to his mansion. When Emperor Taizong's wife Empress Zhangsun died later that year, she, in her final words, pointed out to Emperor Taiizong that Fang had faithfully served him throughout his career and should not be removed. After her death, Emperor Taizong restored Fang to his post.
In 637, as part of Emperor Taizong's scheme to bestow prefectures on his relatives and great generals and officials as their permanent domains, Fang's title was changed to Duke of Liang, and he was given the post of prefect of Song Prefecture , to be inherited by his heirs, although he did not actually report to Song Prefecture but remained at Chang'an as chancellor. Soon, however, with many objections to the system, the strongest of which came from Zhangsun Wuji, Emperor Taizong cancelled the scheme, although Fang's title remained Duke of Liang.
Also in 637, a major revision of the laws, led by Fang, was completed, and the penal laws were said to be far more lenient than the Sui penal code, on which Fang's revision was based, with 500 statutory sections and 20 grades of penalty. Also that year, the code of rites, authored by him and Wei Zheng, was also completed.
In 638, Emperor Taizong, celebrating the birth of a grandson, held a feast for imperial officials, at which he made the comment:
He then awarded both Fang and Wei an imperial sword.
In 639, Emperor Taizong made Fang a senior advisor to Li Chengqian, ordering him to bow to Fang as he would Emperor Taizong. However, Fang was humble and never gave the Crown Prince any opportunity to bow to him, an act of humility that was praised. By this point, Fang's children were also highly honored, and his second son Fang Yi'ai married Emperor Taizong's daughter Princess Gaoyang, while his daughter married Emperor Taizong's brother Li Yuanjia the Prince of Han.
In 641, Fang and Gao Shilian drew rebuke from Emperor Taizong when they inquired of the deputy imperial architect, Dou Desu of imperial construction projects — which Emperor Taizong saw as an encroachment on his liberty. However, Wei pointed out that chancellors were supposed to be responsible for all affairs of state, and Emperor Taizong, realizing that he had erred, was humbled.
in 642, Fang was awarded the additional highly honorable title ''Sikong'' — one of the Three Excellencies. Later that year, when Emperor Taizong was debating between warring with Xueyantuo and making peace by marrying his daughter Princess Xinxing to Xueyantuo's Zhenzhu Khan Yi'nan, Fang was a proponent of peace, and Emperor Taizong agreed, although Emperor Taizong later changed his mind and broke the marriage treaty.
In 643, when Emperor Taizong commissioned the Portraits at Lingyan Pavilion to commemorate the 24 great contributors to Tang rule, Fang's was one of the portraits commissioned.
Later in 643, when Li Chengqian, fearing that Emperor Taizong would depose him in favor of his more favored brother Li Tai the Prince of Wei, was revealed to have plotted with Hou Junji to overthrow Emperor Taizong, Emperor Taizong had Zhangsun, Fang, Xiao Yu, Li Shiji, along with the responsible officials at the legislative and examination bureaus and the supreme court, investigate the matter. They confirmed Li Chengqian's guilt, and Li Chengqian was subsequently deposed, although Emperor Taizong, believing that Li Tai's machinations were responsible for Li Chengqian's downfall, exiled Li Tai as well and created another son, , crown prince. He subsequently made Zhangsun, Fang, and Xiao Li Zhi's senior advisors.
Later that year, Emperor Taizong requested to read the official history that Fang had written about his reign, and Fang initially resisted, stating that it would set a bad precedent . Emperor Taizong disagreed, and Fang thus submitted what he and his staff ahd written. Emperor Taizong noticed that Fang had avoided some of the more sensitive parts of the Incident at Xuanwu Gate, and he ordered Fang to write a more complete and less censored version.
In 645, when Emperor Taizong personally led a campaign against Goguryeo, Fang was put in charge of the capital. It was said that there was an incident where a man approached Fang and stated, "I am here to report that you are committing treason." Fang bound him and delivered him to Emperor Taizong. When the man arrived at Emperor Taizong's camp and accused Fang of treason, Emperor Taizong immediately executed the man and sent an edict to Fang, rebuking him for lack of confidence in himself, stating, "If something like this happens again, you should carry out the decisive act yourself."
In or around 646, Fang was again, for reasons unknown in history but described to be minor reasons, removed from his post and returned to his mansion. After Chu Suiliang submitted a petition stating that Fang's faithfulness should not be forgotten, Emperor Taizong visited Fang's mansion — a visit that Fang anticipated and had cleaned his house for — and he had Fang accompany him on the imperial wagon back to palace. Fang was said to be so influential in Emperor Taizong's decision‐making by this stage that an amusing incident occurred in 647, involving the official Li Wei . Emperor Taizong, then at his summer palace Cuiwei Palace , leaving Fang again in charge of Chang'an, had commissioned Li Wei as the minister of finances. When a messenger arrived from Chang'an, Emperor Taizong asked the messenger what Fang had said about Li Wei, and the messenger responded, "When Fang Xuanling heard that Li Wei was made a minister, all he stated was, 'Li Wei has a handsome beard.'" Emperor Taizong, realizing that Fang was making a veiled comment that Li was not a proper minister, demoted Li Wei to the post of prefect of Luo Prefecture .
In 648, the ''Book of Jin'', the offiial history of , with Fang as its lead editor, was completed. Also in 648, when Emperor Taizong was at another summer palace, Yuhua Palace , he again left Fang in charge of Chang'an, when Fang grew ill. Emperor Taizong summoned him to Yuhua Palace and had the imperial servants attend to him. For a while, Fang got better, and then grew worse. In his illness, Fang believed that the only danger that the empire faced at that point was Emperor Taizong's anger at Goguryeo, and therefore wrote an earnest petition urging the cessation of campaigns against Goguryeo. When Emperor Taizong saw the petition, he stated to Princess Gaoyang, "He is this ill, and yet he still worries so much about my empire." He personally visited Fang to bid farewell, and in fall 649, Fang died at Yuhua Palace and was buried near Empress Zhangsun's tomb .
The Tang Dynasty historian Liu Fang made these comments about Fang and Du Ruhui:
However, the honors the Fang clan received would not last long after Fang Xuanling's death. Emperor Taizong himself died in 649 and was succeeded by Li Zhi . In 653, Fang Yi'ai and Princess Gaoyang were accused of plotting, along with the general Xue Wanche , Emperor Gaozong's uncle Li Yuanjing the Prince of Jing, Li Ke the Prince of Wu, and another brother‐in‐law of Emperor Gaozong, Chai Lingwu , to overthrow Emperor Gaozong and making Li Yuanjing emperor. Fang Yi'ai was executed, and his older brother Fang Yizhi was demoted to be a county sheriff in modern Guangdong. Fang Xuanling, whom Emperor Gaozong had ordered to be worshipped at the imperial shrine with Emperor Taizong, was removed from the imperial shrine.