I am looking for a code example for JsonReaderWriterFactory.CreateJsonWriter
to convert XML to JSON without having to install external dll's. But all examples like this utilize DataContractJsonSerializer
and convert XML like this:
<temp>42</temp>
to produce JSON like:
"\"<temp>42<\\/temp>\""
instead of, what I am really looking for:
{"temp":42}
Apparently, both outputs are JSON, but how do I obtain the latter kind?
By the way, my input is an XElement, not XmlElement.
I already found code to convert JSON to XML using the opposite method JsonReaderWriterFactory.CreateJsonReader
, which works kind of nice, and does NOT utilize DataContractJsonDeserializer
or something like that. So I guess I need to find sample code for JsonReaderWriterFactory.CreateJsonWriter
without DataContractJsonSerializer
. Unfortunately, the MSDN pages like this on CreateJsonWriter do not show code samples.
Update:
I am giving up on this question. Apparently, I should need to switch to the NewtonSoft dll.
The purpose of my question was to quickly create a conversion to translate a sample xml data file, and it is always nice to have a new tool in my personal Lib. Now I will just use an online conversion tool for this one xml.
Thanks all of you for pointing out that I really need to get the Newtonsoft dll if I want to do further JSON stuff.
Comments:
similar to possible duplicate questions: very likely that others tried something like I did, because it sounds attractive, but there is no conclusive answer that it is just not possible
other seemingly duplicate questions almost always focus on the Newtonsoft dll, while my question is on the native dll's.
Duplicate:
Looks so, but I point out that the output is some wrong kind of correct JSON, and ask for a way of calling that method to get 'useful' JSON.
The commenters point out details about why this happens, namely the native dll has limitations.