Reputation: 1
I'm trying to read an .XML file online and then write it into the DataGridView. Downloading the file works. But how can i put the downloaded data to my DataGridView.
VB.NET Code:
Dim xmlfile As String
Dim WebC As New System.Net.WebClient
Dim datas As New DataSet
Try
xmlfile = WebC.DownloadString("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
datas.ReadXml(xmlfile)
DataGridView1.DataSource = datas.Tables("cube")
Catch ex As Exception
MsgBox(ex.Message)
End Try
and this is the .XML File: https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
thanks for every help..
I expect the .xml file to be displayed in the DataGridView. Because I want to display all currency rates.
Upvotes: 0
Views: 360
Reputation: 34421
Put data into a datatable than make the datagridview1.DataSource = dt;
vb.net
Imports System
Imports System.Xml
Imports System.Xml.Linq
Imports System.Data
Module Program
Const URL As String = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
Sub Main(args As String())
Dim dt As New DataTable
dt.Columns.Add("currenct", GetType(String))
dt.Columns.Add("rate", GetType(Decimal))
Dim doc As XDocument = XDocument.Load(URL)
Dim ns As XNamespace = doc.Root.GetDefaultNamespace()
For Each cube As XElement In doc.Descendants(ns + "Cube").Where(Function(x) Not x.Attribute("currency") Is Nothing)
Dim currency As String = CType(cube.Attribute("currency"), String)
Dim rate As Decimal = CType(cube.Attribute("rate"), Decimal)
dt.Rows.Add(New Object() {currency, rate})
Next
End Sub
End Module
c# solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication2
{
class Program
{
const string URL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("currenct", typeof(string));
dt.Columns.Add("rate", typeof(decimal));
XDocument doc = XDocument.Load(URL);
XNamespace ns = doc.Root.GetDefaultNamespace();
foreach(XElement cube in doc.Descendants(ns + "Cube").Where(x => x.Attribute("currency") != null))
{
string currency = (string)cube.Attribute("currency");
decimal rate = (decimal)cube.Attribute("rate");
dt.Rows.Add(new object[] { currency, rate });
}
}
}
}
Upvotes: 1