Skip to content

Instantly share code, notes, and snippets.

@micheletolve
Last active August 2, 2022 14:55
Show Gist options
  • Save micheletolve/4b511875bfff23fe6970960d6ec3d175 to your computer and use it in GitHub Desktop.
Save micheletolve/4b511875bfff23fe6970960d6ec3d175 to your computer and use it in GitHub Desktop.
How to remove #cdata-section when convert xml to json using Linq
string xml = '<Profile><First_Name><![CDATA[Luke]]</First_Name><Last_Name><![CDATA[Skywalker]]</Last_Name><guid><![CDATA[D521C0A3-B6BB-4EA0-AEC6-07860BFE9313]]></guid></Profile>';
string json = string.Empty;
if (!string.IsNullOrEmpty(response_xml))
{
var doc = XElement.Parse(response_xml);
var node_cdata = doc.DescendantNodes().OfType<XCData>().ToList();
foreach (var node in node_cdata)
{
node.Parent.Add(node.Value);
node.Remove();
}
return json = JsonConvert.SerializeXNode(doc, Newtonsoft.Json.Formatting.None, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment