如何将 XML 文档上传到 FTP 位置

本文关键字:FTP 位置 文档 XML | 更新日期: 2023-09-27 18:32:48

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

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

UploadXMLToFTP(XmlDocument xml)
//Now this XMLDocument should be uploaded to ftp without saving in physical drive.

如何将 XML 文档上传到 FTP 位置

遵循如何在 .NET 中通过 FTP 上传文件的 MSDN 示例:

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