Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

Custom Xml Serializer for a specific type in asp.net web api

I am in asp.net web api. In an API method, I am calling an external web service, that returns XML response. I don't want to deserialize it. I would rather like to send it to the client as is. Initially, I am storing the response in XDocument object but when my client specifies application/xml as accept header, I see the following exception

Type 'System.Xml.Linq.XDeclaration' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

How do I get around this problem

Upvotes: 1

Views: 1638

Answers (1)

Elyor
Elyor

Reputation: 865

Great Q,i simple write your problem use in api member:

 [HttpGet]
 [ActionName("Books")]
 public HttpResponseMessage MyBook()
    {
        var request = new HttpResponseMessage(HttpStatusCode.OK);
        var doc = XDocument.Parse(@"<books><book><author>MS</author><name>ASP.NET</name></book></books>");
        request.Content = new StringContent(doc.ToString(), Encoding.UTF8, "application/xml");

        return request;
    }

Try this member source.

Upvotes: 3

Related Questions