Web服务xml货币 [英] Web service xml currency

查看:52
本文介绍了Web服务xml货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要ac #metod,它根据desc返回基于desc的速率作为此XML文件的输入参数, http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml [ ^ ]

i是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);
                 
              }
          }
      }

推荐答案

XPath是你的朋友,在这里' '是一个从该来源获取费率的小应用程序;



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);
        }
    }
}





希望这有帮助,

Fredrik Bornander



Hope this helps,
Fredrik Bornander


这篇关于Web服务xml货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆