I have a handler that uploads a KML file and returns JSON with the KML file as an attribute:
context.Response.Write("{\"name\":\"" + FileName +
"\",\"type\":\"" + FileType +
"\",\"size\":\"" + FileSize +
"\",\"region_id\":\"" + regionID +
"\",\"kml\":\"" + HttpUtility.HtmlEncode(xmlData) + "\"}");
As you can see, I'm trying to encode the KML with HttpUtility.HtmlEncode
but I get an error in my response:
uncaught exception: Invalid JSON
How can I property encode the XML/KML file in C# so I can later decode it in JavaScript?
Edit #1: per Cheeso's comment
I'm using ASP.NET, .NET Version 4 on IIS 7.5 Windows 7. My handler is a ashx file. The response works fine if I leave out the KML data (HttpUtility.HtmlEncode(xmlData)
) from the response.
Edit #2
I also tried using System.Web.Script.Serialization.JavaScriptSerializer
per the moderator's comment. I used it like such:
System.Web.Script.Serialization.JavaScriptSerializer serializer;
context.Response.Write("{\"name\":\"" + FileName +
"\",\"type\":\"" + FileType +
"\",\"size\":\"" + FileSize +
"\",\"region_id\":\"" + regionID +
"\",\"kml\":\"" + serializer.Serialize(xmlData) + "\"}");
I still get the same "Invalid JSON" error.