Click here to Skip to main content
Rate this: bad
good
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
Edited 13-Dec-12 4:00am
v2
Comments
Richard MacCutchan at 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 at 15-Dec-12 11:46am
   
THX
Fredrik Bornander at 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
0 DamithSL 265
1 CPallini 235
2 OriginalGriff 233
3 Maciej Los 195
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
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