1

I have a WCF service that is returning an XElement, this is working fine however I would like it to include the XML Declaration in the response:

<?xml version="1.0" encoding="utf-8"?>

The client side is not something that I can change and is reporting a "Result is not XML" The only other differences between the response of my HTTP result is the content type of my WCF service:

Content-Type: application/xml; charset=utf-8

vs.

Content-Type: text/xml; charset=utf-8

I assume the "Result is not XML" is being caused by the missing XML Declaration. How is it possible to add the XML Declaration to the XElement response? I thought a MessageFormatter might be able fix this, however I have no idea where to start.

Luke
  • 6,195
  • 11
  • 57
  • 85
  • 1
    An XML Declaration is not required in XML. That's not the problem. – John Saunders Aug 11 '09 at 03:34
  • I have written a WCF client which forgets the XML declaration. The SharePoint web-service replies SERVER_ERROR. If I take the exact request and send it after an XML declaration, the web service replies correctly. So, I agree the standards say the XML declaration is not required, but you'll have to agree that Microsoft does not respect standards. Who knows how the client was written? I think it can be the problem. – rds Mar 02 '11 at 17:40
  • How to change the charset ? i want iso-8859-6 data how to do? – user1237131 Jan 09 '13 at 06:08

1 Answers1

0

See XDocument Class Overview for a quick sample of how to add the xml declaration to your output.

Though I have to agree with @John, its unlikely the missing xml declaration is the problem unless the client is manually validating the XML as text (which would be a silly thing to do but I've seen it done) and is assuming that any text missing the xml declaration must not be valid xml.

If the client is a .NET client, then enable WCF message logging and verify what the client is receiving. If the client is not a .NET client, then use Netmon or Wireshark to inspect the traffic to validate payload.

Zach Bonham
  • 6,759
  • 36
  • 31
  • More importantly, if the XML declaration were present, then the XML would be invalid. It would be wrapped by another element, which would be invalid. – John Saunders Aug 17 '09 at 15:35
  • 1
    John, can you explain this comment? I'm just curious what you mean here. This might be my ignorance of XML, but how does putting the XML declaration at the head of the document invalidate it? – Anderson Imes Aug 28 '09 at 18:08
  • Are there ways of doing this without generating the XML manually? For instance when using MemberContracts? – Godspark Sep 05 '16 at 08:17