Pedro
Pedro

Reputation: 1

How do I return a WCF webservice without having the Serialization Error

I'm getting an error after acessing my webservice like:

Server Error in '/' Application.

Type 'System.Xml.XmlDocument' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.

The SRC is fairly easy:

public interface IService1
{
 [OperationContract]
 [WebGet(ResponseFormat = WebMessageFormat.Xml)]
 XmlDocument TwitterGetPublicTimeline();
}

and the WebService:

    public XmlDocument TwitterGetPublicTimeline()
    {            
        var t = new Yedda.Twitter();
        return t.GetPublicTimelineAsXML();
    }

if I return it has a string the document is preceded by " wich is not acceptable.. :|

Upvotes: 0

Views: 2038

Answers (2)

decyclone
decyclone

Reputation: 30840

You can change your method to

    public String TwitterGetPublicTimeline()
    {
        var t = new Yedda.Twitter();
        return t.GetPublicTimelineAsXML().InnerXml;
    }

to return just a String and on the client side, use XmlDocument.LoadXml() method to convert it back to XmlDocument.

Upvotes: 2

Darren Lewis
Darren Lewis

Reputation: 8498

XmlDocument isn't serializable (as you've found out). You should also avoid returning framework types on a WS call for interop. Best thing to do is return the xml as a string and let the client load it into a client specific DOM or return a serializable custom type.

Upvotes: 2

Related Questions