C# wcf 返回一个 XmlDocument?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/964870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wcf return an XmlDocument?
提问by Blaze
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.
我有一个 WCF 服务,其中我使用 XmlWriter 构建了一个 XML 块。完成后,我希望 WCF 将其作为 XmlDocument 返回。
But if I have XmlDocument in the [OperationContract] it doesnt work:
但是,如果我在 [OperationContract] 中有 XmlDocument,则它不起作用:
[OperationContract]
XmlDocument GetNextLetter();
The WCF test utility gives:
WCF 测试实用程序提供:
System.Runtime.Serialization.InvalidDataContractException: Type 'System.Xml.XmlDocument' cannot be serialized.
System.Runtime.Serialization.InvalidDataContractException:类型“System.Xml.XmlDocument”无法序列化。
采纳答案by Samuel Hyman
If you are using .Net 3.5 then you can try returning XElementinstead - this implements IXmlSerializable, which is the missing ingredient needed to make it work with DataContractSerializer.
如果您使用的是 .Net 3.5,那么您可以尝试返回XElement- 这实现了IXmlSerializable,这是使其与 DataContractSerializer 一起工作所需的缺失成分。
回答by Spence
Don't send the XMLDocument, because you can reconstruct it on the other end.
不要发送 XMLDocument,因为您可以在另一端重建它。
You should probably send down the string that you want, or construct a business object which can be serialized to XML and transmit that.
您可能应该发送您想要的字符串,或者构建一个可以序列化为 XML 并传输它的业务对象。
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.
如果您有 XSD 并且想要从中创建可以序列化的业务对象,请查看带有 .net 框架的 XSD.exe 工具。
回答by Szymon Rozga
回答by maelo
append xmlserializer on what you did in the operational contract
将 xmlserializer 附加到您在操作合同中所做的工作
[OperationContract,XmlSerializerFormat]
XmlDocument GetNextLetter();
this will do it !
这会做到的!