Jeffrey van Dijk

Jeffrey van Dijk

  • NA
  • 18
  • 5.1k

FTP get xml file and convert to XmlDocument

Feb 21 2017 10:29 AM
  1. FtpWebRequest fwr = (FtpWebRequest)WebRequest.Create("ftp://**********/httpdocs/App/" + MainWindow.admRelatie + "/" + filename);  
  2.             fwr.Credentials = new NetworkCredential(Username, Password);  
  3.             fwr.UseBinary = true;  
  4.             fwr.Method = WebRequestMethods.Ftp.DownloadFile;  
  5.             FtpWebResponse response = (FtpWebResponse)fwr.GetResponse();  
  6.             Stream s = response.GetResponseStream();  
  7.   
  8.             StreamReader reader = new StreamReader(s);  
  9.   
  10.             string x = reader.ReadToEnd();  
  11.   
  12.             MessageBox.Show(x);  
  13.   
  14.             XmlDocument xml = new XmlDocument();  
  15.   
  16.             xml.Load(s);  
That is the code I am currently using to get the file from the FTP but I am always getting an error saying illegal characters, if I then debug the xml string, I see there is still \n\r and a lot of spaces everywhere in the XML. I at first thought it was the encoding but even if I explicitly say its UTF-8 it still doesn't work, if I then do use the reader.Readline(); instead of read to end and just store every line in a List then it gets rid of the '\n\r' but it still has a lot of empty spaces in the xml which causes the illegal character problem.
 
Then ofcourse I could get rid of the empty spaces by doing str.Replace(" ", ""); but then the problem is that some of values inside the xml also have spaces I do not want to get rid of.
 
I woud love some help :) 

Answers (3)