I have data access method that retrieves xml from database and returns back to business layer. What should be the return type? Should I simply return them as a string? What is the best practice when returning xml from one layer to another?
I will be returning that xml back to client or may be convert them to json based on client request.
I’d argue that this decision depends on what use cases should cover your method.
When I’d return a string:
Code invoking your method isn’t going to manipulate the result as XML, so why wasting CPU and memory loading a complex object like XmlDocument or XDocument, or whatever? just return a string!
Returned XML is a serialized object. You’re going to parse this XML string into an actual .NET object.
When I’d return an XML object like
XDocumentorXmlDocument:Code invoking your method is going to manipulate the result as XML. This includes adding new nodes, attributes, or just read some elements or attributes in your XML.
Code invoking your method uses some .NET base class library or third-party library that needs an XML object as argument.
UPDATE
I forgot to mention the part of “JSON or XML” depending on client request.
Obviously, if your client needs XML, just return your data as XML, and if your client needs JSON, just return your data as JSON.