Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i'm trying to save xml file it was not saving with data.When i'm working in local it was working fine but when i'm trying same in the server it was not working properly.

Please help me.Here is my sample code.
C#
HttpRequest request = this.Request;
            MemoryStream requestStream = new MemoryStream();
            TextWriter writer = new StreamWriter(requestStream);
            var settings = new XmlWriterSettings {Indent = true};
            XmlWriter xwtr = XmlWriter.Create(requestStream, settings);
            writer.Write(request.HttpMethod);
            writer.Write(" ");
            writer.Write(request.Path);
            writer.Write(request.Url.Query);
            writer.Write(" ");
            writer.WriteLine(request.ServerVariables["SERVER_PROTOCOL"]);
            writer.WriteLine(request.ServerVariables["ALL_RAW"]);
            writer.Flush();
            if (request.ContentLength > 0)
            {

                byte[] buffer = new byte[request.ContentLength];
                request.InputStream.Read(buffer, 0, request.ContentLength);
                requestStream.Write(buffer, 0, request.ContentLength);
                MemoryStream xmlStream = new MemoryStream(buffer);

                XmlDocument xmlDoc = new XmlDocument();
                xmlStream.Write(buffer, 0, buffer.Length);
                xmlStream.Position = 0;
                xmlDoc.LoadXml(Encoding.UTF8.GetString(xmlStream.ToArray()));
                xmlStream.Close();
                xmlStream.Dispose();
                string s1 = Request.Cookies["crdid"].Value.ToString();
                string filename = Server.MapPath("xml/" + s1 + ".xml");
                if (File.Exists(filename))
                {
                    // C:\dir\otherDir\possiblefile -> ok
                    File.Delete(Server.MapPath("xml/" + s1 + ".xml"));
                }

                xmlDoc.Save(HttpContext.Current.Server.MapPath("xml/") + s1 + ".xml");
Posted
Updated 27-Nov-13 12:15pm
v4
Comments
Kalyan Ramisetti 27-Nov-13 12:40pm    
I really stuck anyone please help me.....
Richard MacCutchan 27-Nov-13 13:02pm    
You need to provide more details of what is happening, we cannot guess.
Kalyan Ramisetti 27-Nov-13 15:32pm    
Here when i'm trying to save xml file it was not saving with data.In local it is saving with data but in server it was not saving with data.
CHill60 27-Nov-13 16:30pm    
Can you confirm ... the file *is* created where you expect it but is 0 bytes long and no error messages are produced from your code?
Kalyan Ramisetti 27-Nov-13 19:19pm    
Yes,File is created with 0 bytes and no error messages.

1 solution

Sorry I took so long, had to dig this up, but this is what I use to write a XML file from a memory stream. I use this for 2 purposes, One to transmit to the rate server, and Two to write to the drive for troubleshooting a bad rate.

I was thinking that you wrote the file, but the writer did not have any data to write, so you wrote an empty XML file. You can test that theory by looking at the XML data before you write it to the disk drive.

VB
'// What ever your XML is, I use a Class to represent the XML
Dim request As RateService_V12.RateRequest = New RateService_V12.RateRequest()
request.value = "such and such" 

'// Then I register a serializer
Dim response_serializer As XmlSerializer = Nothing
response_serializer = New XmlSerializer(GetType(RateService_V12.RateReply))

'// Then I register a writer, you can call it whatever you want
Dim request_writer As StreamWriter = Nothing

'// Set the writers path
request_writer = New StreamWriter(HttpContext.Current.Server.MapPath("~\App_Data\Shipping\Logs\FEDEX\RateRequest_" & Order_Number & ".xml"))

'//Serialize it
request_serializer.Serialize(request_writer, request)

'//Close the writer and write the file if the write exist
If Not (request_writer Is Nothing) Then request_writer.Close()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900