I am trying to convert below xml containing html code to json using Newtonsoft json in c sharp,
<Content>
<richtext> <![CDATA[<p> <strong>This is sample richtext content </strong> </p> ]]</richtext>
<htmlcontent><![CDATA[ <p> <strong>This is html content </strong> ]]</p> </htmlcontent>
<others> sample </others>
</Content>
My C# code is
string xmlContent = @"<Content><richtext><![CDATA[ <p> <strong>This is sample richtext content </strong> </p> ]]></richtext><htmlcontent> <![CDATA[<p> <strong>This is html content </strong> </p> ]]></htmlcontent><others> sample </others></Content>";
doc.LoadXml(xmlContent);
string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("JSON is :" + jsonText);
My output is
{
"Content": {
"richtext": {
"#cdata-section": " <p> <strong>This is sample richtext content </strong> </p> "
},
"htmlcontent": {
"#cdata-section": "<p> <strong>This is html content </strong> </p> "
},
"others": " sample "
}
}
My expected output is
{
"Content": {
"richtext": "<p> <strong>This is sample richtext content </strong> </p>",
"htmlcontent": "<p> <strong>This is html content </strong> </p>",
"others": " sample "
}
}
Is there any way to remove the #cdata-section element in XML during JSON conversion.
` isn't HTML it's an xml node `p`. If you want to embed HTML into XML you need to use CData
– Liam Nov 02 '17 at 11:58