0

I'm using the xignite API to get real time currency exchange data. When I use my query string:

http://globalcurrencies.xignite.com/xGlobalCurrencies.xml/GetRealTimeRate?Symbol=GBPEUR&_token=[mytoken]

I get the following:

<Rate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://www.xignite.com/services/">
    <Outcome>Success</Outcome>
    <Identity>Request</Identity>
    <Delay>0.0218855</Delay>
    <BaseCurrency>USD</BaseCurrency>
    <QuoteCurrency>EUR</QuoteCurrency>
    <Symbol>USDEUR</Symbol>
    <Date>08/24/2016</Date>
    <Time>3:23:34 PM</Time>
    <QuoteType>Calculated</QuoteType>
    <Bid>0.889126</Bid>
    <Mid>0.88915</Mid>
    <Ask>0.889173</Ask>
    <Spread>4.74352E-05</Spread>
    <Text>
        1 United States dollar = 0.88915 European Union euro
    </Text>
    <Source>Rate calculated from EUR:USD</Source>
</Rate>

I'm trying to access the contents of the Mid element and so far I'm doing this

var xDoc = XDocument.Load(
    "http://globalcurrencies.xignite.com/xGlobalCurrencies.xml/GetRealTimeRate?Symbol="
    + "GBP" + "EUR" + "&_token=[MyToken]");
string s = (string)xDoc.Root.Element("Mid");
output.Text = s;

The xDoc variable returns with the XML that I showed previously, but when I try to get the contents of the Mid element, string s is null. How do I access the contents of the element Mid using XDoc?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62

2 Answers2

0

I use Linq to XML, here is an example

XNamespace ns = "http://www.xignite.com/services/";
XDocument xdoc = XDocument.Load(xmlPath);
var rateMids = (from obj in xdoc.Descendants(ns + "Rate")
                 select new Rate
                 (
                      obj.Attribute("Outcome").Value,
                      obj.Attribute("Identity").Value,
                      (decimal)obj.Attribute("Delay").Value,
                      obj.Attribute("BaseCurrency").Value,
                      obj.Attribute("QuoteCurrency").Value,
                      obj.Attribute("Symbol").Value,
                      DateTime.Parse(obj.Attribute("Date").Value),
                      obj.Attribute("Time").Value.ToString("hh:mm:ss tt", CultureInfo.CurrentCulture),
                      obj.Attribute("QuoteType").Value,
                      (decimal)obj.Attribute("Bid").Value,
                      (decimal)obj.Attribute("Mid").Value,
                      (decimal)obj.Attribute("Ask").Value,
                      Convert.ToInt32(obj.Attribute("Spread").Value),
                      Convert.ToInt32(obj.Attribute("Text").Value)
                 )
                ).ToArray();

myObjects will contain an array of MyObjects from the XML file.

Edit: since you updated your question with the XML, I think you are only missing the namespace on the query (the ns on my query), please take a look on Charles Mager answer

My answer is a different approach.. you save the Rate object and the use it without the need to read the XML again (you would need to define the Rate in a class) Be careful with the value conversions I did, you would need to match yourclass :)

miguelmpn
  • 1,859
  • 1
  • 19
  • 25
  • I'm trying this out but I'm having trouble with ns, reg and MyObject What are they? – user3711612 Aug 24 '16 at 17:50
  • The code in the question also uses LINQ to XML and it doesn't work. I'm not sure how a generic example will help with discovering what's wrong with the current code. – Charles Mager Aug 25 '16 at 08:16
  • @user3711612 there was an error with my previous code, I fixed it... MyObject in my example would be your Rate object, you could extract and instantiate rate objects straight from the xml and then access the Mid property in the Rate object – miguelmpn Aug 25 '16 at 08:59
0

A qualified name in XML is made up of two parts: a namespace and a local name. In your XML, your local name is Mid, but your namespace is not empty: it's http://www.xignite.com/services/, as denoted by the default namespace declaration xmlns="..." in the root element.

If you take this into account, you will get a result:

XNamespace ns = "http://www.xignite.com/services/";
var mid = (decimal)xDoc.Root.Element(ns + "Mid");
Charles Mager
  • 25,735
  • 2
  • 35
  • 45