Mark Allison
Mark Allison

Reputation: 7228

How to load XML into a DataTable?

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?

Upvotes: 0

Views: 7530

Answers (2)

David
David

Reputation: 73564

That is a different question than the original, so to answer the modified question...

.NET generally uses your system's settings. I believe your default network proxy settings are configured in Internet Explorer. In other words, if you set it in IE, then Windows in general will use those settings.

I'd try setting the proxy in Internet Explorer and see if your app can access it from your app.

I did find another possibility here: http://www.musicalnerdery.com/net-programming/reading-an-xml-document-from-behind-a-proxy.html

Upvotes: 1

David
David

Reputation: 73564

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.

Upvotes: 3

Related Questions