如何将XMLDocument上传到ftp位置

时间:2016-02-01 13:45:12

标签: c# xml xsd ftp

我有一个XML文档。我不想在物理路径中保存该xml。如何在内存中上传到包含xml文档的ftp。

所以,我想知道是否有可能在内存中有一个对象并保存到ftp。我有上传到ftp的代码,它将本地路径和远程路径作为参数并上传。

UploadXMLToFTP(XmlDocument xml)

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

1 个答案:

答案 0 :(得分:1)

遵循如何通过FTP在.NET上传文件的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","janeDoe@contoso.com");

        // 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();
    }
}