Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将xml直接上载到ftp_C#_Xml_Forms_Login_Ftp - Fatal编程技术网

C# 将xml直接上载到ftp

C# 将xml直接上载到ftp,c#,xml,forms,login,ftp,C#,Xml,Forms,Login,Ftp,我把这个直接放在按钮下面: XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("Login"); XmlElement id = doc.CreateElement("id"); id.SetAttribute("userName", usernameTxb.Text); id.Se

我把这个直接放在按钮下面:

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Login");
            XmlElement id = doc.CreateElement("id");
            id.SetAttribute("userName", usernameTxb.Text);
            id.SetAttribute("passWord", passwordTxb.Text);
            XmlElement name = doc.CreateElement("Name");
            name.InnerText = nameTxb.Text; 
            XmlElement age = doc.CreateElement("Age");
            age.InnerText = ageTxb.Text;
            XmlElement Country = doc.CreateElement("Country");
            Country.InnerText = countryTxb.Text;
            id.AppendChild(name);
            id.AppendChild(age);
            id.AppendChild(Country);
            root.AppendChild(id);
            doc.AppendChild(root);

            // Get the object used to communicate with the server.  
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://users.skynet.be");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.UsePassive = false;
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("fa490002", "password");
            // Copy the contents of the file to the request stream.  
            StreamReader sourceStream = new StreamReader();
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close(); 

            MessageBox.Show("Created SuccesFully!");
            this.Close();
但我总是得到一个streamreader路径的错误,我需要在那里放置什么? 会议是,创建一个帐户,当我按下按钮时,一个xml文件被保存到,
文件名来自usernameTxb.Text+“.xml”。

下一行没有指向文件或流:

StreamReader sourceStream = new StreamReader();
编辑

下面,我对@dzendras发布的内容进行了扩展。如果这对你有帮助,请接受@dzendras的回答

        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("Login");
        XmlElement id = doc.CreateElement("id");
        id.SetAttribute("userName", usernameTxb.Text);
        id.SetAttribute("passWord", passwordTxb.Text);
        XmlElement name = doc.CreateElement("Name");
        name.InnerText = nameTxb.Text;
        XmlElement age = doc.CreateElement("Age");
        age.InnerText = ageTxb.Text;
        XmlElement Country = doc.CreateElement("Country");
        Country.InnerText = countryTxb.Text;
        id.AppendChild(name);
        id.AppendChild(age);
        id.AppendChild(Country);
        root.AppendChild(id);
        doc.AppendChild(root);

        //Request needs to be in the format: ftp://example.com/path/file.xml or ftp://example.com/file.xml
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://users.skynet.be/" + usernameTxb.Text + ".xml");
        //Specify that we're uploading a file
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UsePassive = false;
        request.Credentials = new NetworkCredential("fa490002", "password");

        //Get raw access to the request stream
        using (Stream s = request.GetRequestStream())
        {
            //Save the XML doc to it
            doc.Save(s);
        }

        //Push the request to the server and await its response
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        //We should get a 226 status code back from the server if everything worked out ok
        if (response.StatusCode == FtpStatusCode.ClosingData){
            MessageBox.Show("Created SuccesFully!");
        }else{
            MessageBox.Show("Error uploading file:" + response.StatusDescription);
        }
        response.Close();

        this.Close();
  • 将URL更改为要保存XML的特定文件名
  • 发送代码:

            using ( Stream s = request.GetRequestStream() )
            {
                doc.Save( s );
            }
            MessageBox.Show( "Created SuccesFully!" );
    

  • 它显示了一个错误,即没有接受0参数的构造函数路径是什么?然后它说“给定路径的形式不受支持”