Sebastian
Sebastian

Reputation: 4811

Save an xml file on server WPF

I have an xml file on server and url is like

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

And I am using the below code to read xml on a wpf application and is working fine

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);

While submitting the xml to the remote url to save I am getting the error

URI formats are not supported.

Is it permitted to saved files on a server from a desktop app like this? Or anything wrong in the code [ if its possible to save an xml on server]

I checked the file permissions of file on server and its Read Write enabled

Upvotes: 0

Views: 761

Answers (1)

Kamil Solecki
Kamil Solecki

Reputation: 1117

As suggested in comments by @Smartis, you should use the FTP protocol to save the file to the server. It can be done as shown below:

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();
    }
}

Here, just use the method, providing the xml file as a parameter.

Upvotes: 2

Related Questions