xml文件未更新 [英] xml file not updating

查看:103
本文介绍了xml文件未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在尝试保存xml文件时,它没有保存数据。当我在本地工作时它工作正常但是当我在服务器中尝试相同时却无法正常工作。



请帮助我。这是我的示例代码。

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.

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");

推荐答案

对不起,我花了很长时间,不得不深入研究,但这是我用来从内存流中编写XML文件的方法。我将它用于2个目的,一个用于传输到速率服务器,两个用于写入驱动器以便对故障率进行故障排除。



我以为你写的是该文件,但编写器没有任何数据要写,所以你写了一个空的XML文件。您可以在将XML数据写入磁盘驱动器之前查看XML数据来测试该理论。



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.

'// 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()


这篇关于xml文件未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆