user1237131
user1237131

Reputation: 1863

How to avoid html codes in wcf xml response?

i wrote one method for getting details.in rest client getting response like this.

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">&lt;Meetings&gt;&#xD;
&lt;Meeting&gt;&#xD;
&lt;Id&gt;1&lt;/Id&gt;&#xD;
&lt;Name&gt;Meeting1&lt;/Name&gt;&#xD;
&lt;Place&gt;SR Nagar&lt;/Place&gt;&#xD;
&lt;Time&gt;12/4/12 12:30pm&lt;/Time&gt;&#xD;
&lt;/Meeting&gt;&#xD;
&lt;Meeting&gt;&#xD;
&lt;Id&gt;2&lt;/Id&gt;&#xD;
&lt;Name&gt;Meeting2&lt;/Name&gt;&#xD;
&lt;Place&gt;Begumpet&lt;/Place&gt;&#xD;
&lt;Time&gt;12/4/12 1:00pm&lt;/Time&gt;&#xD;
&lt;/Meeting&gt;&#xD;
&lt;/Meetings&gt;&#xD;
</string>

in rendered html getting proper.

<Meetings> <Meeting> <Id>1</Id> <Name>Meeting1</Name> <Place>SR Nagar</Place> <Time>12/4/12 12:30pm</Time> </Meeting> <Meeting> <Id>2</Id> <Name>Meeting2</Name> <Place>Begumpet</Place> <Time>12/4/12 1:00pm</Time> </Meeting> </Meetings>

How to handle it in code in wcf to avoid <

Upvotes: 0

Views: 839

Answers (2)

Allon Guralnek
Allon Guralnek

Reputation: 16141

In your [OperationContract] method, instead of returning a string, return an array of Meeting objects. The Meeting class should contain the properties you'd like to return:

[DataContract]
public class Meeting
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Place { get; set; }
    // etc...
}

If you're using .NET 4.0 and up, the [DataContract] and [DataMember] attributes are not required.

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

Your service operation returns string and you are writing XML to that string. It will always look like that and browser shows it correctly only because it hides string tag and unescapes the content.

To return real XML you must not use string as return value. Try to use for example XElement.

Upvotes: 3

Related Questions