Compress data sent to Webservice
I am sending a XMLDocument to my webservice (.asmx) and when i check the file size of that xmldocumetn by xmldocument.outerxml.tostring().length it is coming around 433434 ( i dont know what does it represents...is it in bytes) So i want to compress this data from client and send it to webservice without saving the file as XML in my local harddrive.. Can anybody throw some light on this... T开发者_C百科hanks in Advance
xmldocument.OuterXml.ToString().Length
will return the length of the xml string, like "something".Length
will return 9. So no, it is not in bytes.
The actual size in byte will depend on your encoding, for exemple you can use:
System.Text.ASCIIEncoding.Unicode.GetByteCount(s);
System.Text.ASCIIEncoding.ASCII.GetByteCount(s);
...
Depending on the encoding you are using.
EDIT1: Is the amount of data significant ? Assuming you're sending it over the wire, you have many options to compress data before sending it.
You can use you own custom encoding to reduce the data sent as much as possible, like say indexing constant node names to "a", "b"... and sending the map after. You can also try to send it in chunk.
If file size is an issue because of timeout, I recommend looking into the webservice.timeout property On MSDN
You probably want to investigate System.IO.Compression.GZipStream. Your web service will need to know to decompress it using the same class.
Do some benchmarking before and after the compression. You may find that the zipping/unzipping at each end of the pipe adds more time than what you saved by chopping some bytes from the payload. You need to find the sweet spot where zipping saves you more time than it costs (1/2 a MB, especially over a LAN, is not a lot of traffic, unless you are really pumping those xml documents through).
I'd some bad experience with .NET compression libraries in past, so I recommend SharpZipLib. You'll need to change your web method implementation to accept a byte[]
;
You can also to access that service as WCF service, using a TCP binding, so it'll consume less bandwidth. You should consider this option before messing with your web method signature.
精彩评论