Question

I am trying to save XML file on a server and send it to another remote over FTP. To create/save XML file, I am using

XmlWriter writer = XmlWriter.Create("d:\\path\\to\\folder\\filename.xml");

and upload to FTP.

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(filename);

Everything works fine. It creates XML file, save to folder, send to FTP correctly. But now I am in a trouble while deploying the code. We have a load balancer and two prod servers. so I cannot be sure where will it save at the moment. and since it may not be available at the other server, I might get 404.

Is there a way I can specify servername before file location when I save XML file? Basically I want a common location specifier to look into like hostname/ipaddress along with implemented code already.

Was it helpful?

Solution

I solved this issue in a different way. I couldn't figure out a common location so I tried to eliminate the need itself for a common location.

I used MemoryStream object and stored my XML file in memory instead of server physically and sent it from there. That way no need to save to local path. More example code is as below.

 StringBuilder builder = new StringBuilder() ;                
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = Encoding.UTF8;
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(builder, settings))
         {                    
            writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
            writer.WriteStartElement("mainElem");
            writer.WriteStartElement("elem");
            writer.WriteElementString("var1", "data");
            writer.WriteElementString("var2", "data");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }
 byte[] a = System.Text.Encoding.UTF8.GetBytes(builder.ToString());
 MemoryStream stream = new MemoryStream(a);
 return stream;

Posting it here in case someone else come across similar situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top