3

I am sending xml/mathml as data in my AJAX request, and at the server side in C# I get this sort of text:

%3Cmath%3E%0A%20%20%20%20%3Cmrow%3E%0A%20%20%20%20%20%20%20%20%3Cmsub%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cmi%3Ex%3C%2Fmi%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cmtext%3E12%3C%2Fmtext%3E%0A%20%20%20%20%20%20%20%20%3C%2Fmsub%3E%0A%20%20%20%20%20%20%20%20

So, basically it is xml, but the xml basic characters are converted to url like characters, %20, %3E, %0A etc...

I have this POST method in my API controller:

[HttpPost]
public HttpResponseMessage PostUpload(HttpRequestMessage req)
{
    string jsonContent = req.Content.ReadAsStringAsync().Result;
    Utility.Utility.WriteLineToConsole("json data post: " + jsonContent);

    return Request.CreateResponse(HttpStatusCode.OK, jsonContent);
} 

The Utility function WriteLineToConsole() prints the jsonContent and the top text among the result.

How can i covert the string above to xml, i.e replace the url-like characters to xml characters?

Note: I am using MVC 4 / C# , jQuery AJAX, and both contentType and dataType are of type json. I want my data object to be like

data:{mathml: "<math>...</math>"}
Vlad
  • 2,739
  • 7
  • 49
  • 100
  • 1
    That looks like it's `urlencode`d. Try `Server.Decode(thatString);` or `HttpUtility.UrlDecode(thatString)` or `Uri.UnescapeDataString(thatString)` – Kyle Jun 08 '17 at 11:02
  • 1
    Seems contentType is not properly set in ajax call. Check [this](https://stackoverflow.com/questions/17535872/http-post-xml-data-in-c-sharp) – Prashant Shirke Jun 08 '17 at 11:04
  • 1
    BTW, why are you setting it to json when you're obviously sending xml? Seems `application/xml; charset=UTF-8` is more appropriate. – Kyle Jun 08 '17 at 11:05
  • @Kyle, `Uri.UnescapeDataString` will work well - https://dotnetfiddle.net/zLkawf. Could even be marked as duplicate of https://stackoverflow.com/questions/12898556/decode-percent-encoded-string-c-sharp-net, I guess. – kiziu Jun 08 '17 at 11:05
  • I think you need to use System.Net.WebUtility.HtmlDecode & System.Net.WebUtility.HtmlEncode. Html won't except certain special characters : en.wikipedia.org/wiki/… – jdweng Jun 08 '17 at 11:16

1 Answers1

2
HttpUtility.UrlDecode(thatString)

did the job

Vlad
  • 2,739
  • 7
  • 49
  • 100