Last active
August 2, 2022 14:55
-
-
Save micheletolve/4b511875bfff23fe6970960d6ec3d175 to your computer and use it in GitHub Desktop.
How to remove #cdata-section when convert xml to json using Linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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