WebClient sending an XDocument

Posted on 2011-10-10
Last Modified: 2013-12-16
I have an application which is sent an XML Document from a web page.  The idea is to act on this XML file, update it as necessary and then send the file back.  Now the initial send is done from a web page using Response.Write and is read by a C# WebClient object.  All good so far.

However I'm not sure how best to send the resultant XML file back to the web service.  I am working in a Windows Mobile 7 application and would prefer to use the WebClient object.  

I have constructed the XML file (including some embedded images as CData sections) and effectively just want to call a URL passing the XML in some form.  Obviously not as a query string but how?
Question by:ChrisMDrew
    LVL 96

    Expert Comment

    by:Bob Learned
    I haven't done any Windows 7 development, so I am wondering why you are using the WebClient, instead of adding a web reference, and using the generated proxy to call a web method?

    Author Comment

    The main reason is I expect this to be ported to Android and as such I don't want to be tied into a Microsoft environment.  If I stay with WebClient / URI's that will convert easier.  Also I'm not writing the web service side of things - this is a traditional ASP web site.
    LVL 96

    Accepted Solution

    Since the web service should be able to handle HTTP POST and GET, I wouldn't see any problem with the WebClient approach.  The default web service has those options disabled, though, so you would need to check if it is enabled.

    The WebClient.UploadString uses an HTTP POST.

    Client.UploadString Method (String, String)

    Author Comment

    I have implemented the web post using the following :-

    string webServiceURL = IsolatedStorageHelper.GetObject<string>("blu3server");
    string page = "whereabouts_update.aspx";

    // Now issue an asynchronous request to post updated data back to the server
    Uri serviceUri = new Uri(webServiceURL + page, UriKind.Absolute);
    WebClient webClient = new WebClient();

    // Upload this to the server
    webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
    webClient.UploadStringAsync(serviceUri ,xDocument.ToString(SaveOptions.DisableFormatting));

    Which seems to work - I have a dummy .aspx file which does nothing but in the webClient_UploadStringCompleted event there are no errors returned.  My continuing problem is

    1> How do I recover the posted data in the target web page
    2> If there is an error how does the target web page report this to the caller?

    Probably obvious but not so far to me!
    LVL 96

    Expert Comment

    by:Bob Learned
    I don't understand what you mean by " How do I recover the posted data in the target web page"...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now