views:

402

answers:

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.

A: 

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