Getting the contents of an XML element using XDoc in C#

120 Views Asked by At

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?

2

There are 2 best solutions below

3
On

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

0
On

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