I will present two ways of getting an XML from a WebService and then read it as object(s).

The first one will use validation. To validate an XML we generally use a XSD. This XSD can be provided in the XML it self as a reference or not (like in this case). The XSD is really usefull here to auto-generate a C# class we can use later to represent the values from the XML in objects.

The WebService used in this example provides the Rating for Currencies (eurofxref).

This is the resulting XML:

<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
   <gesmes:subject>Reference rates</gesmes:subject>
      <gesmes:name>European Central Bank</gesmes:name>
      <Cube time="2016-01-08">
         <Cube currency="USD" rate="1.0861"/>
         <Cube currency="JPY" rate="128.51"/>
         <Cube currency="BGN" rate="1.9558"/>
         <Cube currency="CZK" rate="27.022"/>
         <Cube currency="DKK" rate="7.4598"/>
         <Cube currency="GBP" rate="0.74519"/>
         <Cube currency="HUF" rate="315.53"/>
         <Cube currency="PLN" rate="4.3523"/>
         <Cube currency="RON" rate="4.5275"/>
         <Cube currency="SEK" rate="9.2640"/>
         <Cube currency="CHF" rate="1.0860"/>
         <Cube currency="NOK" rate="9.6810"/>
         <Cube currency="HRK" rate="7.6427"/>
         <Cube currency="RUB" rate="80.4134"/>
         <Cube currency="TRY" rate="3.2491"/>
         <Cube currency="AUD" rate="1.5495"/>
         <Cube currency="BRL" rate="4.3685"/>
         <Cube currency="CAD" rate="1.5321"/>
         <Cube currency="CNY" rate="7.1577"/>
         <Cube currency="HKD" rate="8.4312"/>
         <Cube currency="IDR" rate="15111.30"/>
         <Cube currency="ILS" rate="4.2597"/>
         <Cube currency="INR" rate="72.4206"/>
         <Cube currency="KRW" rate="1304.78"/>
         <Cube currency="MXN" rate="19.3231"/>
         <Cube currency="MYR" rate="4.7615"/>
         <Cube currency="NZD" rate="1.6450"/>
         <Cube currency="PHP" rate="51.204"/>
         <Cube currency="SGD" rate="1.5606"/>
         <Cube currency="THB" rate="39.387"/>
         <Cube currency="ZAR" rate="17.3870"/>

So now we have the XML. But still no XSD…

To do that you just have to open this XML in Visual Studio, then in the Menu click on XML then Create Schema. Here it will generate two schema (two XSD’s) according to what we have in the XML.

Great! We have the XSD files. But what now?

Use XSD.EXE program to generate the C# class (I use a BAT file so I can regenerate in a click):

call “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat”

xsd.exe eurofxref-daily.xsd eurofxref-daily1.xsd /c /n:NameSpaceToDefine


Now all we have to do is import the generated CS file in your Project and do something like:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace XmlParser
    class Program
        static void Main(string[] args)
            var currencies = new List<Currency>();
            var ser = new XmlSerializer(typeof(Envelope));
            const string filename = @"D:\temp\eurofxref-daily.xml";

            var xmlObj = ser.Deserialize(new FileStream(filename, FileMode.Open)) as Envelope;

            if (xmlObj != null && xmlObj.Cube != null && xmlObj.Cube.Cube1 != null && xmlObj.Cube.Cube1.Cube != null)
                foreach (var entry in xmlObj.Cube.Cube1.Cube)
                    var currency = new Currency
                        Country = entry.currency,
                        Rate = entry.rate


            //Display the result
            foreach (var currency in currencies)
                Console.WriteLine(string.Format("{0}: {1}", currency.Country, currency.Rate));


    class Currency
        public string Country;
        public decimal Rate;

OR you could bypass all this procedure and simply use the XML as a XmlDocument (so without the XSD).

var result = new List<Currency>();
var doc = new XmlDocument();


var root = doc.DocumentElement;
var dateNode = root.ChildNodes[2].ChildNodes[0];
var date = dateNode.Attributes["time"].Value;
var currencyNodes = root.ChildNodes[2].ChildNodes[0].ChildNodes;

foreach (XmlNode node in currencyNodes)
      var country = node.Attributes["currency"].Value;
      var rate = node.Attributes["rate"].Value;
      result.Add(new Currency
         CurrencyId = Guid.NewGuid(),
         DateTime = DateTime.Parse(date),
         Country = country,
         Rate = decimal.Parse(rate.Replace('.', ','))

These two procedures are the easiest but the first one is the safest thank to the validation provided by the XSD.