0

I have IEnumerable<XElement> rates with the following XML/ Data in it.

<results>
  <rate id="AUDEUR">
    <Name>AUD to EUR</Name>
    <Rate>0.6602</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.6604</Ask>
    <Bid>0.66</Bid>
  </rate>
  <rate id="AUDGBP">
    <Name>AUD to GBP</Name>
    <Rate>0.5391</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.5393</Ask>
    <Bid>0.5389</Bid>
  </rate>
  <rate id="AUDUSD">
    <Name>AUD to USD</Name>
    <Rate>0.9051</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.9054</Ask>
    <Bid>0.9048</Bid>
  </rate>
  <rate id="EURAUD">
    <Name>EUR to AUD</Name>
    <Rate>1.5147</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.5153</Ask>
    <Bid>1.5141</Bid>
  </rate>
  <rate id="EURGBP">
    <Name>EUR to GBP</Name>
    <Rate>0.8166</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.8167</Ask>
    <Bid>0.8165</Bid>
  </rate>
  <rate id="EURUSD">
    <Name>EUR to USD</Name>
    <Rate>1.3709</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.371</Ask>
    <Bid>1.3709</Bid>
  </rate>
  <rate id="GBPAUD">
    <Name>GBP to AUD</Name>
    <Rate>1.8549</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.8556</Ask>
    <Bid>1.8541</Bid>
  </rate>
  <rate id="GBPEUR">
    <Name>GBP to EUR</Name>
    <Rate>1.2246</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.2247</Ask>
    <Bid>1.2244</Bid>
  </rate>
  <rate id="GBPUSD">
    <Name>GBP to USD</Name>
    <Rate>1.6788</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.679</Ask>
    <Bid>1.6787</Bid>
  </rate>
  <rate id="USDAUD">
    <Name>USD to AUD</Name>
    <Rate>1.1049</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.1052</Ask>
    <Bid>1.1045</Bid>
  </rate>
  <rate id="USDEUR">
    <Name>USD to EUR</Name>
    <Rate>0.7294</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.7295</Ask>
    <Bid>0.7294</Bid>
  </rate>
  <rate id="USDGBP">
    <Name>USD to GBP</Name>
    <Rate>0.5957</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.5957</Ask>
    <Bid>0.5956</Bid>
  </rate>
</results>
    base {System.Xml.Linq.XContainer}: <results>
  <rate id="AUDEUR">
    <Name>AUD to EUR</Name>
    <Rate>0.6602</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.6604</Ask>
    <Bid>0.66</Bid>
  </rate>
  <rate id="AUDGBP">
    <Name>AUD to GBP</Name>
    <Rate>0.5391</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.5393</Ask>
    <Bid>0.5389</Bid>
  </rate>
  <rate id="AUDUSD">
    <Name>AUD to USD</Name>
    <Rate>0.9051</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.9054</Ask>
    <Bid>0.9048</Bid>
  </rate>
  <rate id="EURAUD">
    <Name>EUR to AUD</Name>
    <Rate>1.5147</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.5153</Ask>
    <Bid>1.5141</Bid>
  </rate>
  <rate id="EURGBP">
    <Name>EUR to GBP</Name>
    <Rate>0.8166</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.8167</Ask>
    <Bid>0.8165</Bid>
  </rate>
  <rate id="EURUSD">
    <Name>EUR to USD</Name>
    <Rate>1.3709</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.371</Ask>
    <Bid>1.3709</Bid>
  </rate>
  <rate id="GBPAUD">
    <Name>GBP to AUD</Name>
    <Rate>1.8549</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.8556</Ask>
    <Bid>1.8541</Bid>
  </rate>
  <rate id="GBPEUR">
    <Name>GBP to EUR</Name>
    <Rate>1.2246</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.2247</Ask>
    <Bid>1.2244</Bid>
  </rate>
  <rate id="GBPUSD">
    <Name>GBP to USD</Name>
    <Rate>1.6788</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.679</Ask>
    <Bid>1.6787</Bid>
  </rate>
  <rate id="USDAUD">
    <Name>USD to AUD</Name>
    <Rate>1.1049</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>1.1052</Ask>
    <Bid>1.1045</Bid>
  </rate>
  <rate id="USDEUR">
    <Name>USD to EUR</Name>
    <Rate>0.7294</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.7295</Ask>
    <Bid>0.7294</Bid>
  </rate>
  <rate id="USDGBP">
    <Name>USD to GBP</Name>
    <Rate>0.5957</Rate>
    <Date>2/17/2014</Date>
    <Time>12:07am</Time>
    <Ask>0.5957</Ask>
    <Bid>0.5956</Bid>
  </rate>
</results>

Based on this data I need to add the records in DB. The following values are required to add:

  1. id from <rate id="AUDEUR">, in this case it will be AUDEUR
  2. Rate from <Rate>0.6602</Rate>, in this case it will be 0.6602
  3. Date from <Date>2/17/2014</Date>

I have not much experienced with XML, can you please help me how I can get these values ?

Thanks

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
Toubi
  • 2,469
  • 10
  • 33
  • 49
  • try converting the xml to document type then you can parse it. http://stackoverflow.com/a/55840/2334391 http://www.w3schools.com/xml/default.asp – jos Feb 17 '14 at 05:30

3 Answers3

0

You can use Linq2Xml

XElement doc=XElement.Load(url);
var elements=doc.Elements("rate")
                .Where(x=>
                         x.Attribute("id").Value=="AUDEUR" &&
                         (double)x.Element("Rate")>=0.6602 &&
                         DateTime.Compare((DateTime)x.Element("Date"),DateTime.Parse("2/17/2014"))>=0
                      )
                .Select(e=>
                            new
                           {
                                Id=e.Attribute("id").Value,
                                Name=e.Element("Name").Value,
                                Rate=e.Element("Rate").Value....
                           }
                       );

Now you can iterate over elements like this

foreach(var elm in elements)
{
    elm.Name;
    elm.Rate;
}
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

I would create a class to handle rates:

class Rate
{
    public string Id { get; private set; }
    public decimal Value { get; private set; }
    public DateTime Date { get; private set; }

    // let's make the class immutable
    public Rate(string id, decimal value, DateTime date)
    {
        Id = id;
        Value = value;
        Date = date;
    }
}

and use LINQ to XML to parse the XML:

private static IEnumerable<Rate> GetRates(XElement source)
{
    return from r in source.Element("results").Elements("rate")
           select new Rate(
               (string)r.Attribute("id"),
               (decimal)r.Element("rate"),
               (DateTime)r.Element("date"));
}

I'm not sure why you said you had IEnumerable<XElement>, but assuming that's what you have, you can easily get rates from IEnumerable<XElement> by:

var data = rates.SelectMany(x => GetRates(x)).ToList();

it will give you List<Rate> which you can use to update your database.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Let's represent a rate record like class below:

public class RateRecord {
    public string Id { get; set; }
    public string Rate { get; set; }
    public string Date { get; set; }
}

And get the list of parsed rates:

var rateRecords = new List<RateRecord>();
foreach(var rate in rates) {
    rateRecords.Add(new RateRecord {
        Id = rate.Attribute("id").Value,
        Rate = rate.Element("Rate").Value,
        Date = rate.Element("Date").Value
    });
}

Also you can write a method for parsing:

public IEnumerable<RateRecord> ParseRates(IEnumerable<XElement> rates) {
    foreach(var rate in rates) {
        yield return new RateRecord {
            Id = rate.Attribute("id").Value,
            Rate = rate.Element("Rate").Value,
            Date = rate.Element("Date").Value
        };
    }
}