Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p><a href="http://msdn.microsoft.com/fr-fr/library/system.io.stream.read.aspx" rel="nofollow">http://msdn.microsoft.com/fr-fr/library/system.io.stream.read.aspx</a></p> <p>buf is a byte[] array, which, once the method has returned, contains the data that you just Read. BUFFER_SIZE is the size of the data you want to read. If you want to read a single byte, BUFFER_SIZE=1. If you want to read one kilobyte of data, BUFFER_SIZE=1024, etc. Note that, if you ask for a too big buffer (e.g. asking for 1MB when the data is 1KB), it won't matter much. It will read a KB, and return.</p></li> <li><p>Your final string should look like this, unless XE.com decides to change it: </p> <p>XE.com: USD to EUR rate: 1.00 USD = 0.716372 EUR</p> <p>You can use the String methods to strip things you don't need: The whole first part </p> <p><code>(XE.com: USD to EUR rate:)</code> </p> <p>can be easily removed by just building a string with your data:</p> <p><code>(string header = "XE.com: {0} to {1} rate:", currency1, currency2)</code></p> <p>,then calling <code>String.Replace(header, '')</code>. From there, you can call <code>String.Split('=')</code>, splitting at the '=' sign, then removing the currency part from the splitted strings (again, <code>String.Replace()</code>) and finally calling <code>Double.TryParse()</code></p></li> </ol> <p>Note: codesparkle's method is way easier, because you basically skip step 1. But XE.com are not providing an API: You have no guarantee the returned string will be valid, or won't change someday in the future.</p> <p>OK, here's some code:</p> <pre><code>private double GetConvertedCurrencyValue(string inputCurrency, string outputCurrency, double value) { string request = String.Format(http://www.xe.com/ucc/convert.cgi?Amount={0}&amp;From={1}&amp;To={2}", value, inputCurrency, outputCurrency); System.Net.WebClient wc = new System.Net.WebClient(); string apiResponse = wc.DownloadString(request); // This is a blocking operation. wc.Dispose(); /* Formatting */ // Typical response: "XE.com: curr1 to curr2 rate: x curr1 = y curr2" // The first part, up until "x curr1" is basically a constant string header = String.Format("XE.com: {0} to {2} rate:" inputCurrency, outputCurrency); // Removing the header // The response now looks like this: x curr1 = y curr2 apiResponse = apiResponse.Replace(header, ""); // Let's split the response at '=', to retrieve the right part string outValue = apiResponse.Split('=')[1]; // Getting rid of the 'curr2' part outValue = outValue.Replace(outputCurrency, ""); return Double.Parse(outValue, System.Globalization.CultureInfo.InvariantCulture); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload