I can't seem to parse XML nodes from an URL. The project (MVC .net) I'm currently working on has a model with the data i want to bring :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyHR.Domain.Models
{
public class ExchangeRate
{
public string DataCurenta { get; set; }
public string Moneda { get; set; }
public string Valoarea { get; set; }
}
}
Then this is my controller (I've tried different kind of methods including nodes, but I've stopped at this one) :
using MyHR.Domain.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
namespace MyHR.Web.Controllers
{
public class ExchangeRateController : Controller
{
// GET: ExchangeRate
public ActionResult DisplayXml(List<ExchangeRate> exchangeRates)
{
List<ExchangeRate> data = new List<ExchangeRate>();
data = ReturnData();
return View(data);
}
public List<ExchangeRate> ReturnData()
{
string xmldata = @"http://www.bnr.ro/nbrfxrates.xml";
XDocument Xml = XDocument.Load(xmldata);
XDocument doc = new XDocument();
DataSet ds = new DataSet();
ds.ReadXml(xmldata);
//Loop through the selected Nodes.
//XmlNodeList xmlDate = doc.GetElementsByTagName("Cube");
//XmlNodeList listdata = doc.GetElementsByTagName("Rate");
var ratelist = new List<ExchangeRate>();
ratelist = (from ReturnData in doc.Descendants("Cube").Descendants()
select new ExchangeRate
{
DataCurenta = ReturnData.Element("date").ToString(),
//Moneda = ReturnData.Element("currency").ToString(),
//Valoarea = ReturnData.Element("description").ToString(),
}).ToList();
return ratelist;
}
}
}
This is the XML I'm trying to get info from : http://www.bnr.ro/nbrfxrates.xml
Also, the view would look like this:
@using MyHR.Domain.Models
@model IEnumerable<MyHR.Domain.Models.ExchangeRate>
@{
/**/
ViewBag.Title = "DisplayXml";
}
<br><br />
<h1 align="center">Cursul valutar din data curenta</h1>
<br><br />
<meta name="Curs Valutar" content="width=device-width" />
<title>Index</title>
<table class="table table-responsive table-bordered">
<thead style="background-color:#88D0AA">
<tr align="center">
<th align="center">
DataCurenta
</th>
<th align="center">
Moneda
</th>
<th align="center">
Valoarea
</th>
</tr>
</thead>
<tbody>
@foreach (ExchangeRate exchange in Model)
{
<tr>
<td>@exchange.DataCurenta</td>
<td>@exchange.Moneda</td>
<td>@exchange.Valoarea</td>
</tr>
}
</tbody>
</table>
How would I search for the nodes of XML file and also bring these nodes in the view? The nodes needed are the date, currency and the value of currency.
Thank you in advance for your support.