1

I have following simple xml to json conversion code

XmlDocument xmlDocument=new XmlDocument();
xmlDocument.LoadXml("<Root><Record><Column>1</Column></Record></Root>");
string val=JsonConvert.SerializeXmlNode(xmlDocument,Formatting.None);

xml does get converted but the value contains some characters which json invalid.

value contains following

"{\"Root\":{\"Record\":{\"Column\":\"1\"}}}

I do not want those "\" characters in the converted string. Am I missing something here?

2 Answers 2

1

Am I missing something here?

I suspect you're missing the fact that they're not really there :) I'm sure you're just seeing them in the debugger - which escapes things like quotes for you.

Just print the string to the console, and you'll see what you want.

(I've just tried it myself using your sample code, and it's fine.)

Sign up to request clarification or add additional context in comments.

4 Comments

Jon, thanks for the quick response. But that is the exact text returned to client and also that is the value of string which suggest that they are there.
I am returning the val to client and this is what I get in Fiddler HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Tue, 08 Jan 2013 04:19:52 GMT X-AspNet-Version: 4.0.30319 Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: application/json; charset=utf-8 Content-Length: 44 Connection: Close "{\"Root\":{\"Record\":{\"Column\":\"1\"}}}"
@user1901680: Then it sounds like you're double-encoding the data. The value in val is correct. (As I say, log it and you'll see it's fine.) You haven't shown how you're returning the data to the client, but if you've declared that you'll return a string and that you want the result to be in JSON, that's probably the issue.
I think you are right. But I don't know how to fix or what to do. Here is the web api code public string Get() { //return (new ResultResponse()).GetData(); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml("<Root><Record><Column>1</Column></Record></Root>"); string val = JsonConvert.SerializeXmlNode(xmlDocument, Newtonsoft.Json.Formatting.None); return val; }
0

I'm not sure but just try this.. Instead of using Character's use the Predeclared Entity.

Character   Predeclared Entity
&              &amp;
<              &lt;
>              &gt;
"              &quot;
'              &apos;

For example, the Record name “AT&T” should appear in the XML markup as “AT&T”: the XML parser will take care of changing “&” back to “&” automatically when the document is processed.

Anyone Correct me if I'm wrong...

Thanks.,

2 Comments

You might have realised by now that stackoverflow does HtmlDecode your responses
No, the JSON encoding is working fine - the problem is how the OP is returning the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.