0

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...

serhads
  • 462
  • 2
  • 7
  • 22

1 Answers1

1

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
maka
  • 566
  • 4
  • 11
  • No I wanna see that"

    " exactly same format in XML or Json output like "

    "
    – serhads Mar 01 '12 at 09:27
  • It is not possible to have HTML in an XML file like that. The <, > and " cannot be used in the XML file but there are some others too. Please look at the answer in: http://stackoverflow.com/questions/730133/invalid-characters-in-xml – maka Mar 01 '12 at 09:39
  • Thank you maka. I will check that answer, by the way do you have any idea that is it possible with JSON? – serhads Mar 01 '12 at 09:46
  • First, i recomend reading http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work as people might stop answering your questions otherwise:) To answer what is legal in JSON, read http://stackoverflow.com/questions/983451/where-can-i-find-a-list-of-escape-characters-required-for-my-json-ajax-return-ty There is also some info there how to write the stream. – maka Mar 01 '12 at 09:57