I want to load an XML file on the internet into a DataTable in C#. The XML is loaded from http://rates.fxcm.com/RatesXML and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Rates>
<Rate Symbol="EURUSD">
<Bid>1.29174</Bid>
<Ask>1.29198</Ask>
<High>1.29407</High>
<Low>1.28723</Low>
<Direction>-1</Direction>
<Last>14:56:48</Last>
</Rate>
<Rate Symbol="USDJPY">
<Bid>82.862</Bid>
<Ask>82.885</Ask>
<High>83.293</High>
<Low>82.847</Low>
<Direction>1</Direction>
<Last>14:56:47</Last>
</Rate>
<!-- More like the above -->
</Rates>
Can I use the ReadXml method of the DataTable class to read the XML, or do I need some kind of http request to get it first into a string?
EDIT:
I’ve just written the following
public DataTable GetCurrentFxPrices(string URL)
{
DataSet ds = new DataSet("fxPrices");
ds.ReadXml(URL);
}
and it is trying to read the data, but I am behind a corporate firewall. I’m really clueless now how to get around this. I get this error:
System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
In firefox I have an HTTP proxy set with a port number. Can I set that somewhere in my app?
Yes, you can use ReadXml if the XML is in a single-table format.
It may not come in in the format you expect, so you may need to explore the structure of the dataset a bit, but it works just fine.
As a general rule when loading data from an XML file into a Datatable, I’d read it into a DataSet first and be sure it does not create more than one table. Any nesting int he XML usually results in multiple tables in a DataSet.
This particular file, however, looks like it would import into a single table just fine.