将XML数据转换为List

时间:2013-02-14 08:36:23

标签: c# xml

如何将http://www.floatrates.com/daily/USD.xml转换为c#

中的列表

到目前为止,我有:

protected void btnUpdate_Click ( object sender, EventArgs e ) {

    XmlDocument xmlDoc = new XmlDocument ( );
    xmlDoc.Load ( "http://www.floatrates.com/daily/USD.xml" );
    XmlNodeList itemNodes = xmlDoc.SelectNodes ( "//channel/title/link/description/language/pubDate/lastBuildDate/item" );
    foreach ( XmlNode itemNode in itemNodes ) {

        XmlNode currencyNode = itemNode.SelectSingleNode ( "targetCurrency" );
        XmlNode rateNode = itemNode.SelectSingleNode ( "exchangeRate" );




        GridView2.DataSource = lstdata;
        GridView2.DataBind ( );
    }

2 个答案:

答案 0 :(得分:1)

这将做你想要的。修改ds.Tables[0].Rows行以从XML获取所需的数据。

 public static List<string> ProcessXMLDoc()
 {
      XmlDocument xDoc = new XmlDocument();
      xDoc.Load("http://www.floatrates.com/daily/USD.xml");
      List<string> xmlList = new List<string>();
      StringReader sr = new StringReader(xDoc.OuterXml);

      DataSet ds = new DataSet();
      ds.ReadXml(sr);

      foreach (DataRow row in ds.Tables[0].Rows)
      {
           if (!string.IsNullOrWhiteSpace(row[0].ToString()))
           {
                xmlList.Add(row[0].ToString());
           }
      }

      return xmlList;
  }

答案 1 :(得分:1)

我会创建一个保存值的对象,并使用Linq2XML查询xml文档。

    protected void btnUpdate_Click ( object sender, EventArgs e ) 
    {
        GridView2.DataSource = GetFloatRates();
        GridView2.DataBind ( );
    }

    private List<FloatRateItem> GetFloatRates()
    {
        XDocument xmlDoc = XDocument.Load("http://www.floatrates.com/daily/USD.xml");
        var floatRates = xmlDoc.Descendants("channel");

        var items = from i in floatRates.Elements("item")
                    select new FloatRateItem
                    {
                        Title = i.Element("title").Value,
                        Link = i.Element("link").Value,
                        Description = i.Element("description").Value,
                        PubDate = i.Element("pubDate").Value,
                        BaseCurrency = i.Element("baseCurrency").Value,
                        TargetCurrency = i.Element("targetCurrency").Value,
                        ExchangeRate = i.Element("exchangeRate").Value
                    };

        return items.ToList();
    }

    class FloatRateItem
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
        public string PubDate { get; set; }
        public string BaseCurrency { get; set; }
        public string TargetCurrency { get; set; }
        public string ExchangeRate { get; set; }

        public override string ToString()
        {
            return string.Format(@"<item>
<title>{0}</title>
<link>{1}</link>
<description>{2}</description>
<pubDate>{3}</pubDate>
<baseCurrency>{4}</baseCurrency>
<targetCurrency>{5}</targetCurrency>
<exchangeRate>{6}</exchangeRate>
</item>", Title, Link, Description, PubDate, BaseCurrency, TargetCurrency, ExchangeRate);
        }
    }