Martin
Martin

Reputation: 597

Best practice for transferring an XML doc to a web service

I've been asked to do some research on how we could create a web service that will allow a client to transfer an XML document to us over a web service.

I was going to use WCF and use an XmlElement input parameter, but I have no idea if this is best practice or not.

Can anyone recommend some on-line resources to look at or have any suggestions?

Thanks.

Upvotes: 0

Views: 84

Answers (1)

Adriano Repetti
Adriano Repetti

Reputation: 67090

I wouldn't tranfer an XmlElement (even supposing it can be marshaled in your contract, I never tried something like that and you'll just have an useless overhead both in computation and bandwidth).

An XML document is plain text (and then an XML fragment is text too) so best option is to use a simple plain System.String parameter.

Of course a string may not be always the best solution, I'm thinking, for example, about a big XML document. In this case I would use a System.IO.Stream. Start here from MSDN and read this for an example.

To summarize (code from link above) declare your contract like this:

[MessageContract] 
public class UploadFileRequest 
{ 
    [MessageHeader] 
    public string fileName;

    [MessageBodyMember] 
    public Stream fileContents; 
} 

[ServiceContract] 
public interface IFileDownloader 
{ 
    [OperationContract] 
    Stream DownloadFile(string fileName);

    [OperationContract] 
    void UploadFile(UploadFileRequest request); 
}

Upvotes: 1

Related Questions