I would like to import directly from Internet the following XML file in an Object or List that I can read and filter in order to then update a table with the new monthly exchange rate (via a button in WPF).
XML File is: https://www.backend-rates.bazg.admin.ch/api/xmlavgmonth
XML Schema is : https://www.backend-rates.ezv.admin.ch/avgratesmonth.xsd
I searched a solution in MS Documentation and WEB site and tried a lot of commands and variants (Deserialize, Linq, XmlDocument, XmlReader, XmlTextReader and others) but I don't find the right command and parameters to do it correctly. It seems that I'm not able to manage correctly an XML with COMPLEXTYPES and recognize Node or Element or Attributes (see the schema above).
Could someone be so kind and help me?
Thanks in advance.
Here after the code of my last attempt which is not working...
Class generated "automatically in Visual Studio 2022:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cambi
{
// NOTA: con il codice generato potrebbe essere richiesto almeno .NET Framework 4.5 o .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://www.backend-rates.ezv.admin.ch/avgratesmonth")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "https://www.backend-rates.ezv.admin.ch/avgratesmonth", IsNullable = false)]
public partial class monatsmittelkurs
{
private string monatField;
private monatsmittelkursDevise[] deviseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "gYearMonth")]
public string monat
{
get
{
return this.monatField;
}
set
{
this.monatField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("devise")]
public monatsmittelkursDevise[] devise
{
get
{
return this.deviseField;
}
set
{
this.deviseField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://www.backend-rates.ezv.admin.ch/avgratesmonth")]
public partial class monatsmittelkursDevise
{
private string land_deField;
private string land_frField;
private string land_itField;
private string waehrungField;
private decimal kursField;
private string codeField;
/// <remarks/>
public string land_de
{
get
{
return this.land_deField;
}
set
{
this.land_deField = value;
}
}
/// <remarks/>
public string land_fr
{
get
{
return this.land_frField;
}
set
{
this.land_frField = value;
}
}
/// <remarks/>
public string land_it
{
get
{
return this.land_itField;
}
set
{
this.land_itField = value;
}
}
/// <remarks/>
public string waehrung
{
get
{
return this.waehrungField;
}
set
{
this.waehrungField = value;
}
}
/// <remarks/>
public decimal kurs
{
get
{
return this.kursField;
}
set
{
this.kursField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
}
}
Program:
using Cambi;
namespace Cambi
{
class Cambi
{
static void Main()
{
string file = @"C:\Users\digar\Downloads\xmlavgmonth.xml";
List<monatsmittelkurs> monatsmittelkursvars = new XmlSerialize().ConvertXmlToObjects<monatsmittelkurs>(file);
Console.WriteLine(monatsmittelkursvars);
}
public class XmlSerialize
{
public List<T> ConvertXmlToObjects<T>(string filename)
{
List<T> list;
using (StreamReader reader = new StreamReader(filename))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
list = (List<T>)serializer.Deserialize(reader);
}
return list;
}
public T ConvertXmlToObject<T>(string filename)
{
T obj;
using (StreamReader reader = new StreamReader(filename))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
obj = (T)serializer.Deserialize(reader);
}
return obj;
}
}
}
}