Wcf returns an XmlDocument?

I have a WCF service where I create an XML block using XmlWriter. Upon completion, I want WCF to return it as an XmlDocument.

But if I have an XmlDocument in [OperationContract], it does not work:

[OperationContract] XmlDocument GetNextLetter(); 

WCF Test Utility gives:

System.Runtime.Serialization.InvalidDataContractException: The type "System.Xml.XmlDocument" cannot be serialized.

+4
source share
4 answers

If you use .Net 3.5, you can try instead of XElement - this implements IXmlSerializable , which is the missing component needed to work with the DataContractSerializer.

+10
source

add xmlserializer about what you did in the operating contract

 [OperationContract,XmlSerializerFormat] XmlDocument GetNextLetter(); 

it will do it!

+11
source

Do not send XMLDocument because you can restore it on the other end.

You should probably send the string you want, or create a business object that can be serialized in XML and pass it.

Take a look at the XSD.exe tool with the .net framework if you have an XSD and you want to make a business object out of it that can be serialized.

+2
source

DataContractSerializer can serialize XmlElement instances. Therefore, simply return the DocumentElement property of your XmlDocument instance. See: MSDN .

+2
source

Source: https://habr.com/ru/post/1285905/


All Articles