2016-02-01 49 views
0

我有一個XML文檔。我不想將這個xml保存在物理路徑中。我如何上傳到內存中有xml文件的ftp。如何將XMLDocument上傳到ftp位置

所以,我想知道是否有可能在內存中有一個對象並保存到FTP。我有上傳到ftp的代碼,它將本地路徑和遠程路徑作爲參數並上傳它。

UploadXMLToFTP(XmlDocument xml) 

//Now this XMLDocument should be uploaded to ftp without saving in physical drive. 

回答

1

繼如何在.NET通過FTP上傳文件的MSDN Example

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

... 
public static void UploadXMLToFTP (XmlDocument xml) 
{ 
    // Get the object used to communicate with the server. 
    using(FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm")) 
    { 
     request.Method = WebRequestMethods.Ftp.UploadFile; 

     // This example assumes the FTP site uses anonymous logon. 
     request.Credentials = new NetworkCredential ("anonymous","[email protected]"); 

     // Copy the contents of the file to the request stream. 
     request.ContentLength = xml.OuterXml.Length; 

     Stream requestStream = request.GetRequestStream(); 
     xml.Save(requestStream); 
     requestStream.Close(); 


     FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

     Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

     response.Close(); 
    } 
}