保留JSON到XML转换的HTML标记

时间:2017-08-14 08:39:09

标签: c# json xml json.net

我有一个JSON对象,我使用以下代码转换为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>
&lt;p&gt;Democrats also want to “reinvigorate and modernise” US &lt;ft-content type="http://www.stack.com/ontology/content/Article" url="http://api.stack.com/content/d2c32614-61c6-11e7-91a7-502f7ee26895"&gt;antitrust&lt;/ft-content&gt; laws for a broad attack on corporations.&lt;/p&gt;
&lt;p&gt;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. &lt;/p&gt;
&lt;/body&gt;</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>

在JSON的原始"bodyXML"内,存在带有HTML标记的HTML文本,但转换后它们会被压缩为HTML实体。我想要做的是在转换后保留这些HTML标记。

我该怎么做?

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

我认为在xml节点的内部文本中不可能有'编码'HTML标记

但是,在解析XmlDocument之后,可以对该Xml节点的内部文本执行HTML解码。

这将为您提供所有HTML标记完整的文本。

例如,

private static string ConvertFileToXml()
    {
        string fileContent = File.ReadAllText("text.json");
        XmlDocument doc = JsonConvert.DeserializeXmlNode(fileContent, "root");
        return System.Web.HttpUtility.HtmlDecode(doc.SelectSingleNode("root").SelectSingleNode("bodyXML").InnerText);
    }

需要命名空间:System.Web