wcf return an XmlDocument? [duplicate]

百般思念 提交于 2019-12-17 19:51:33

问题


I have a WCF service where Im building up a block of XML using an XmlWriter. Once complete I want to have the WCF return it as an XmlDocument.

But if I have XmlDocument in the [OperationContract] it doesnt work:

[OperationContract]
XmlDocument GetNextLetter();

The WCF test utility gives:

System.Runtime.Serialization.InvalidDataContractException: Type 'System.Xml.XmlDocument' cannot be serialized.


回答1:


If you are using .Net 3.5 then you can try returning XElement instead - this implements IXmlSerializable, which is the missing ingredient needed to make it work with DataContractSerializer.




回答2:


append xmlserializer on what you did in the operational contract

[OperationContract,XmlSerializerFormat]
XmlDocument GetNextLetter();

this will do it !




回答3:


The DataContractSerializer can serialize XmlElement instances. So just return the DocumentElement property of your XmlDocument instance. See: MSDN.




回答4:


Don't send the XMLDocument, because you can reconstruct it on the other end.

You should probably send down the string that you want, or construct a business object which can be serialized to XML and transmit that.

Have a look at XSD.exe tool with the .net framework if you have an XSD and you want to make a business object from it which can be serialized.



来源:https://stackoverflow.com/questions/964870/wcf-return-an-xmldocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!