there is a xml message:
<Data>
<aa>12345\n67890</aa>
<bb>98765\\4321<bb>
<Data>
I need to convert the xml to json:
String strXmlData = xmlHelper.SelectSingleNode(xml,"//Data").OuterXML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strJsonData);
String jsonData = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None)
The it seems that json result is added escape charactor by JsonConvert automatically.
{"aa":"12345\\n67890","bb":"98765\\\\4321"}
I need to keep the value as it is(ie, \n as new line instead of "\n" string). Is there any way to prevent JsonConvert to generate escape charactor? Or is there any suggestion to remove the escape charactor?
Any Suggestion is appreciated, thank you!
CodePudding user response:
I can only suggest a work around using some string functions (or RegEx)
String jsonData = JsonConvert
.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None).Replace("\\\\","\\");
CodePudding user response:
Try doing an XSLT transformation as a preprocessing step to convert the escape sequences prior to conversion.
In my experience the vast majority of XML-to-JSON (and vice-versa) conversions need some custom pre- or post-processing to get the precise output you want. Personally, I usually find it easier to do the conversion "by hand" in XSLT 3.0, which gives you complete control.