如何将XML加载到DataTable? [英] How to load XML into a DataTable?

查看:197
本文介绍了如何将XML加载到DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在互联网上加载一个XML文件到C#中的 DataTable 。 XML从 http://rates.fxcm.com/RatesXML 加载,如下所示:

 <?xml version =1.0encoding =UTF-8?> 
< Rate>
< Rate Symbol =EURUSD>
< Bid> 1.29174< / Bid>
< Ask> 1.29198< / Ask>
< High> 1.29407< / High>
<低> 1.28723< / Low>
< Direction> -1< / Direction>
< Last> 14:56:48< / Last>
< / Rate>
< Rate Symbol =USDJPY>
< Bid> 82.862< / Bid>
< Ask> 82.885< / Ask>
<高> 83.293< /高>
<低> 82.847< /低>
< Direction> 1< / Direction>
< Last> 14:56:47< / Last>
< / Rate>
<! - 更像以上 - >
< / Rates>

我可以使用 DataTable ReadXml > class 读取XML,还是需要某种http请求将其首先转换成字符串?



编辑:
I我刚刚写了以下

  public DataTable GetCurrentFxPrices(string URL)
{
DataSet ds = new DataSet(fxPrices);
ds.ReadXml(URL);

}

它正在尝试读取数据,但我在企业防火墙之后。我现在真的很无知如何解决这个问题。我收到这个错误:



System.Net.WebException:远程服务器返回错误:(407)需要代理验证。



在firefox中,我有一个带有端口号的HTTP代理集。可以将其设置在我的应用程序的某个地方吗?

解决方案

是的,如果XML是单表格式,可以使用ReadXml



它可能不会以您期望的格式输入,因此您可能需要浏览数据集的结构,但它的工作原理很好。 >

作为将数据从XML文件加载到Datatable中的一般规则,我首先将其读入DataSet,并确保它不会创建多个表。任何嵌套XML,通常会导致DataSet中的多个表。



但是,这个特定文件看起来像导入到单个表格很好。 >

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.

这篇关于如何将XML加载到DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆