serhads
serhads

Reputation: 462

HTML serializing issue in WCF Web API to XML and Json

I am using WCF Web API service to publish some data via Json and Xml. But I have a problem with html string to serialize it.

for Xml serialize I use XmlMediaTypeFormatter() and for Json serialize I have CustomJSONMediaTypeFormatter class which uses Newtonsoft.Json library.

I get data with Entity framework from database, and like code bellow I publish it with WCF web api service.

    [WebGet(UriTemplate = "getLogin")]
    public IQueryable<LoginTableDTO> Login()
    {
         var loginList = ltr.GetList();
        List<LoginTableDTO> dtoList = new List<LoginTableDTO>();
        foreach (LoginTable item in loginList)
        {
            dtoList.Add(LoginTableAssembler.ToDTO(item));
        }
    return dtoList.AsQueryable();
}

but with html strings like

<p><input style="float: right" type="submit" name="name" value="Save Changes" /></p>

which are coming from database, after serialize returns

&lt;p&gt;&lt;input style="float: right" type="submit" name="name" value="Save Changes" /&gt;&lt;/p&gt;

for xml and returns

\u003cp\u003e\u003cinput style=\"float: right\" type=\"submit\" name=\"name\" value=\"Save Changes\" /\u003e\u003c/p\u003e

for Json.

As you see they are not human readable.

I also have some problem with datetime object. but I found a solution to convert string before doing that prosses actually, I am not happy with it.(thats another issue).

What should I do for serializing HTML strings like normal strings (which are not have "html tags") to XML and JSON?

Thanks in advance...

Upvotes: 0

Views: 1131

Answers (1)

maka
maka

Reputation: 566

I'm not 100% sure what you want but if you want:

&lt;p&gt;&lt;input style="float: right" type="submit" name="name" value="Save Changes" /&gt;&lt;/p&gt;

to become:

<p><input style="float: right" type="submit" name="name" value="Save Changes" /></p>

use http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx. The problem is that you'll need to escape the HTML to save it to XML....meaning I think the string below looks right.

\u003cp\u003e\u003cinput style=\"float: right\" type=\"submit\" name=\"name\" value=\"Save Changes\" /\u003e\u003c/p\u003e

Upvotes: 1

Related Questions