Click here to Skip to main content
12,625,453 members (36,645 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML
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..

[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 13-Dec-12 3:50am
Updated 13-Dec-12 4: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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100