开发者

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.GetPublicTimelineAsX开发者_开发问答ML();
    }

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜