2016-08-30 45 views
0

我對服務器和URL的XML文件是這樣保存在服務器上WPF的XML文件

http://exampldomain.net:81/sample_xml_sd.xml

,我使用下面的代碼來讀取一個WPF應用程序的XML和工作正常

string xml_path="http://exampldomain.net:81/sample_xml_sd.xml"; 
XmlDocument doc = new XmlDocument(); 
doc.Load(xml_path); 

// Add a price element. 
XmlElement newElem = doc.CreateElement("price"); 
newElem.InnerText = "10.95"; 
doc.DocumentElement.AppendChild(newElem); 

// Save the document to a file. White space is 
// preserved (no white space). 
doc.PreserveWhitespace = true; 
doc.Save(xml_path); 

雖然提交XML到遠程URL保存我收到錯誤

URI格式沒有t支持。

是否允許從這樣的桌面應用程序保存服務器上的文件? 或任何錯誤代碼[如果可以節省服務器的XML]

我檢查服務器上文件的文件權限和讀寫啓用

+0

你的服務器是什麼? IIS? 「它是否允許像這樣從桌面應用程序上保存服務器上的文件」不,您需要額外的代碼,並最終增加服務器配置。 – Tewr

+2

你**不能**通過HTTP操作文件。這將是可怕的。嘗試FTP協議。 – Smartis

+0

你需要保存到一個流而不是文件,有各種支持網絡使用的流量,但FTP可能是最簡單的解決方案 – MikeT

回答

2

爲在@Smartis意見建議,您應該使用FTP協議將文件保存到服務器。它可以做到如下所示:

public static void uploadToFTP (XmlDocument xml) 
{ 
    using(FtpWebRequest request = (FtpWebRequest)WebRequest.Create("your FTP URL")) 
    { 
     request.Method = WebRequestMethods.Ftp.UploadFile; 

     // Insert your credentials here. 
     request.Credentials = new NetworkCredential ("username","password"); 

     // 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(); 
     response.Close(); 
    } 
} 

在這裏,只需使用該方法,提供的XML文件作爲參數。