2011-01-13 129 views
-1

我把這個直接的一個按鈕,如下所示:上傳XML直接到ftp

  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文件被保存到,ftp://users.skynet.be/testxml/ 文件名是從usernameTxb.Text +「.xml」。

回答

2
  1. 更改URL到您想保存XML特定文件名。
  2. 發送代碼:

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

下面這行是不是指向文件或流:

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(); 
+0

它顯示了一個錯誤,有沒有construtors thath取0 argruments – 2011-01-14 11:55:07