0

How can I transform http://www.floatrates.com/daily/USD.xml into a List in c#

So far I have:

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 ( );
    }
1
  • 2
    Well, have you considered creating your own class, with an instance for each element? (I would strongly advise using LINQ to XML if you can.) Commented Feb 14, 2013 at 8:41

2 Answers 2

1

I would create an object to hold the values and use Linq2XML to query the xml document.

    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);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

This will do what you want. Modify the ds.Tables[0].Rows line to get the data you want from the 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 Comment

Thank you for your quick answer. Firstly i would like to retrieve only <targetCurrency></targetCurrency> <exchangeRate></exchangeRate> with the press of a button to my gridview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.