title: C# - Catch WebException While Posting XML Async
body: <p>I am POSTING XML data using WebClient.</p>
  
  <pre><code>  public string uploadXMLData(string destinationUrl, string requestXml)
          {
              try
              {
  
                  System.Uri uri = new System.Uri(destinationUrl);
                  using (WebClient client = new WebClient())
                  {
                      client.Headers.Add("content-type", "text/xml");
                      var response = client.UploadString(destinationUrl, "POST", requestXml); 
                  }
              }
  
              catch (WebException webex)
              {
  
                  WebResponse errResp = webex.Response;
                  using (Stream respStream = errResp.GetResponseStream())
                  {
                      StreamReader reader = new StreamReader(respStream);
                      string text = reader.ReadToEnd();
                  }
              }
              catch (Exception e)
              { }
  
              return null;
          }
  </code></pre>
  
  <p>When there is an error, I catch it as WebException, and I read the Stream in order to know what the XML response is.</p>
  
  <p>What I need to do, is post the XML data to the URL in Async.
  So I changed the function:</p>
  
  <pre><code>public string uploadXMLData(string destinationUrl, string requestXml)
  {
      try
      {
  
          System.Uri uri = new System.Uri(destinationUrl);
          using (WebClient client = new WebClient())
          {
  
              client.UploadStringCompleted
         += new UploadStringCompletedEventHandler(UploadStringCallback2); 
              client.UploadStringAsync(uri, requestXml);
          }
      }
  
      catch (Exception e)
      { }
  
      return null;
  }
  
  
  void UploadStringCallback2(object sender, UploadStringCompletedEventArgs e)
  {            
      Console.WriteLine(e.Error);
  }
  </code></pre>
  
  <p>How can I catch the WebException now and read the XML response?</p>
  
  <p>Can I throw e.Error?</p>
  
  <p>Any help would be appreciated</p>