Uploading an xml direct to ftp
i put this direct below a button:
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();
but i always get an error of the streamreader path, what do i need to place there ? the meening is, creating an account and when i press the button, an xml file is saved t开发者_如何转开发o, ftp://users.skynet.be/testxml/ the filename is from usernameTxb.Text + ".xml".
- Change the URL to specific filename where you want to save your XML.
Sending code:
using ( Stream s = request.GetRequestStream() ) { doc.Save( s ); } MessageBox.Show( "Created SuccesFully!" );
The line below isn't pointing to a file or stream:
StreamReader sourceStream = new StreamReader();
EDIT
Below I've expanded on what @dzendras posted. If this helps you please accept @dzendras's answer.
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();
精彩评论