我有一個JSON對象,我使用以下代碼轉換成XML:保留HTML標記來XML轉換
private string ConvertFileToXml(string file)
{
string fileContent = File.ReadAllText(file);
XmlDocument doc = JsonConvert.DeserializeXmlNode(fileContent, "root");
// Retain html tags.
doc.InnerXml = HttpUtility.HtmlDecode(doc.InnerXml);
return XDocument.Parse(doc.InnerXml).ToString();
}
其中string json
是下列對象:
{
"id": "2639",
"type": "www.stack.com",
"bodyXML": "\n<body><p>Democrats also want to 「reinvigorate and modernise」 US <ft-content type=\"http://www.stack.com/ontology/content/Article\" url=\"http://api.stack.com/content/d2c32614-61c6-11e7-91a7-502f7ee26895\">antitrust</ft-content> laws for a broad attack on corporations.</p>\n<p>Mr Schumer said the Democrats’ new look should appeal to groups that backed Mrs Clinton, such as the young and minority groups, and members of the white working-class who deserted Democrats for Mr Trump. </p>\n</body>",
"title": "Democrats seek to reclaim populist mantle from Donald Trump",
"standfirst": "New economic plan is pitched as an assault on growing corporate power",
"byline": "David J Lynch in Washington",
"firstPublishedDate": "2017-07-24T17:51:25Z",
"publishedDate": "2017-07-24T17:50:25Z",
"requestUrl": "http://api.stack.com/content/e8bec6dc-708d-11e7-aca6-c6bd07df1a3c",
"brands": [
"http://api.ft.com/things/dbb0bdae-1f0c-11e4-b0cb-b2227cce2b54"
],
"standout": {
"editorsChoice": false,
"exclusive": false,
"scoop": false
},
"canBeSyndicated": "yes",
"webUrl": "http://www.stack.com/cms/s/e8bec6dc-708d-11e7-aca6-c6bd07df1a3c.html"
}
和輸出的方法生成此:
<root>
<id>2639</id>
<type>www.stack.com</type>
<bodyXML>
<p>Democrats also want to 「reinvigorate and modernise」 US <ft-content type="http://www.stack.com/ontology/content/Article" url="http://api.stack.com/content/d2c32614-61c6-11e7-91a7-502f7ee26895">antitrust</ft-content> laws for a broad attack on corporations.</p>
<p>Mr Schumer said the Democrats’ new look should appeal to groups that backed Mrs Clinton, such as the young and minority groups, and members of the white working-class who deserted Democrats for Mr Trump. </p>
</body></bodyXML>
<title>Democrats seek to reclaim populist mantle from Donald Trump</title>
<standfirst>New economic plan is pitched as an assault on growing corporate power</standfirst>
<byline>David J Lynch in Washington</byline>
<firstPublishedDate>2017-07-24T17:51:25Z</firstPublishedDate>
<publishedDate>2017-07-24T17:50:25Z</publishedDate>
<requestUrl>http://api.stack.com/content/e8bec6dc-708d-11e7-aca6-c6bd07df1a3c</requestUrl>
<brands>http://api.ft.com/things/dbb0bdae-1f0c-11e4-b0cb-b2227cce2b54</brands>
<standout>
<editorsChoice>false</editorsChoice>
<exclusive>false</exclusive>
<scoop>false</scoop>
</standout>
<canBeSyndicated>yes</canBeSyndicated>
<webUrl>http://www.stack.com/cms/s/e8bec6dc-708d-11e7-aca6-c6bd07df1a3c.html</webUrl>
</root>
在JSO的原始"bodyXML"
內N,有HTML標籤的HTML文本,但是在轉換後它們被壓縮成HTML實體。我想要做的是在轉換後保留這些HTML標籤。
我該怎麼做?
幫助將不勝感激!
你*肯定*這就是你想要做什麼?您所包含的HTML摘錄中的任何錯誤都會導致您的整個XML文件無法解析(實際上,在您的示例中,有一個關閉的'