1

I want to embed an xml string in a json string. I am returning this json from a web method and at client side I have to extract the xml string from this json data. I tried this:

var data= $.parseJSON(jsonResponse);

But as the jsonResponse contains XML data it is becoming an invalid json and becomes unable to parse.

Is there any way to successfully embed xml string in json and extract it ?

EDIT:

Tried encoding xml string :

 System.Security.SecurityElement.Escape(xmlString)

and then added it to json string.

Still at client side the json couldn't be parsed

EDIT tried Ted Johnson's solution and the problem is partially fixed.

Now I could parse the json and extract the other attributes. But on accessing the xml attribute, it says undefined. Also couldn't decode it.

Sharun
  • 3,022
  • 6
  • 30
  • 59

1 Answers1

2

You will need to do the following.

  1. Ensure the XML is encoded to project quote escaping. As the XML will need to be parsed as a string. In c# there is a standard way, URL Encoding using C#
  2. ParseJSON
  3. Access JSON attribute which has the xml encoded as a string and decode it. http://www.w3schools.com/jsref/jsref_decodeuri.asp

  4. Parse the XML ... http://api.jquery.com/jQuery.parseXML/ and save result for use.

Community
  • 1
  • 1
Ted Johnson
  • 4,315
  • 3
  • 29
  • 31
  • Thank you. That worked partially. I mean now I could parse the json and extract the other attributes. But on accessing the xml attribute, it says undefined. Also couldn't decode it – Sharun Nov 27 '13 at 14:01
  • Look at your browser network traffic and validate it from there. The question is why would parseJSON alter the string? My guess it is not being send in the response at all. If you narrow down the problem and are still stumped, share the details. – Ted Johnson Nov 27 '13 at 14:56