0

I am working on a feature that can take xmlstring as an input and uploads it to the server. Below is the code I'm playing around but I saw some answers in the other places suggesting System.io.stream. So, I'm curious about that way of doing it. How can I just store the .xml on the server with/without saving it on the disk.

Code for using file on the Base Directory folder location

string filename = "Sample.XML";                     
List<string> file = new();
file.Add(filename);
//Downloading XML string using web client                    
string Xml = _webClientService.DownloadXml("http://aws.api/Links?=admin");
string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\SampleFiles";
if (!Directory.Exists(fileLocation)) { Directory.CreateDirectory(fileLocation); }

XmlReader reader = XmlReader.Create(Xml);
XDocument doc = XDocument.Load(reader);
doc.Save(fileLocation + $"\\{file}");

I'm yet to work on the upload piece. But the method declaration would look like:

UploadFiles(string filelocation, List<string> fileNames);

2nd method using System.io.stream

 Stream s = _httpService.GetStream("http://aws.api/Links?=admin");
 var file = "sample.gz";
 Stream gZip = new GZipStream(s, CompressionMode.Decompress);
 XmlReader reader = XmlReader.Create(gZip, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment });
 reader.MoveToContent();
 XDocument doc = XDocument.Load(reader);

And for this one, the prototype can be like:

UploadFileStream(string file, Stream strm);

Code for GetStream:

 public static async Task<Stream> GetStream(string url)
    {
        var t = Task.Run(() => httpClient.GetAsync(url));
        t.Wait();
        HttpResponseMessage response = t.Result;

        Stream res;
        // Handle the response
        switch (response.StatusCode)
        {
            case HttpStatusCode.OK:
                res = await response.Content.ReadAsStreamAsync();
                break;
           
            default:
                int statusCode = (int)response.StatusCode;
                throw new HttpException(statusCode, response.ReasonPhrase);
        }

        return res;
    }

Is it possible to avoid gZip conversion & store xmlfile direct to server using the memory stream?

Progman
  • 16,827
  • 6
  • 33
  • 48
Walter
  • 79
  • 6
  • `xmlfile` is by definition a **file**, as the name clearly says. And certainly, you can send an XML stream to your server, if the content is reasonable in size and the server side is written to accept that stream. However, unless the content is pretty small, it wouldn't be practical. How would the server be expected to hold 100s of MB or more in memory, especially from multiple connections? – Ken White May 24 '21 at 00:52
  • Yes you can upload an xml without using GZIP. GZIP is compression and reduces the number of bytes to perform the upload and adds the data as a mime attachment to the body of the response. You can add the xml to the body of an html message provided the the characters are all UTF-8 or add the xml as a mime attachment to the body as text. – jdweng May 24 '21 at 06:03
  • Your examples show downloading xml text from a server, and parsing it as an XDocument. If you plan to immediately upload it again you wouldn't need to parse it. – Jeremy Lakeman May 24 '21 at 06:45
  • @jdweng can you please use the example above & show the code? – Walter May 24 '21 at 21:31
  • @JeremyLakeman how does that work? – Walter May 24 '21 at 21:31
  • When you make a HTTP Request you can ask for the response to be either GZIP or not GZIP. So to get a response that is not GZIP you have to modify the headers in your request. See : https://stackoverflow.com/questions/839888/httpwebrequest-native-gzip-compression – jdweng May 24 '21 at 23:30
  • You can `.PostAsync(...)` with a `new StreamContent(stream)` parameter. Directly from the stream you receive from a separate http request. – Jeremy Lakeman May 25 '21 at 00:43

0 Answers0