Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a c# metod that returns the rate based on the desc as an input parameter from this XML file, http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml[^]
i am new to XML, and i have looked at several exampels, but no luck.

Please help

This is what my method looks like so far, dosent work..

C#
[WebMethod]
        public string test2(string test)
        {

         XmlTextReader reader = new XmlTextReader("http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml");
         
      while(reader.Read())
      {
          if (reader.NodeType == XmlNodeType.Element)
          {
              if (reader.Name == "dailyrates")
              {
                  return reader.GetAttribute(test);
                 
              }
          }
      }
Posted
Updated 13-Dec-12 3:00am
v2
Comments
Richard MacCutchan 13-Dec-12 9:02am    
dosent work.
Which is not a very good description. Please edit your post and explain exactly what results you expect and what you get.

1 solution

XPath is your friend, here's a small app that fetches the rate from that source;

C#
using System;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;

namespace XmlTest
{
    class Program
    {
        static decimal GetRate(string code)
        {
            XPathDocument document = new XPathDocument("http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml");
            XPathNavigator navigator = document.CreateNavigator();

            XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
            manager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XPathNavigator node = navigator.SelectSingleNode(String.Format("/exchangerates/dailyrates/currency[@code='{0}']/@rate", code), manager);
            return Decimal.Parse(node.Value, NumberStyles.AllowDecimalPoint, CultureInfo.GetCultureInfoByIetfLanguageTag("da-DK"));
        }

        static void Main(string[] args)
        {
            decimal rate = GetRate("EUR");
            Console.WriteLine("Rate=" + rate);
        }
    }
}


Hope this helps,
Fredrik Bornander
 
Share this answer
 
Comments
Jesper Schlütter 15-Dec-12 11:46am    
THX
Fredrik Bornander 15-Dec-12 11:54am    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900