I am currently doing a project related to a formula 1 API but sadly I cannot figure out how to serialize my xml content into an object.
The XML I am trying to serialize : https://pastebin.com/nkt9EA1k
My API request and the attempt to serialize it.
client = new HttpClient();
HttpResponseMessage response = client.GetAsync("http://ergast.com/api/f1/2022").Result;
HttpContent responseContent = response.Content;
XmlSerializer serializer = new XmlSerializer(typeof(Wrapper));
Wrapper wrapper2;
string result = File.ReadAllText("temp.txt");//hc.ReadAsStringAsync().Result;
using (TextReader reader = new StringReader(result))
{
wrapper2 = (Wrapper)serializer.Deserialize(reader);
}
if(wrapper2 != null)
{
SecondWrapper wrapper = wrapper2.MRData;
string outPut = wrapper.Season;
foreach (Race r in wrapper.RaceTable)
{
outPut += $"\n{r.RaceName}\t{r.Date}\t{r.Circuit.CircuitName}";
}
File.WriteAllText("Output.txt", outPut);
}
My Wrapper class :
public class Wrapper
{
public SecondWrapper MRData;
}
My SecondWrapper Class :
public class SecondWrapper
{
public string Season;
public Race[] RaceTable;
}
My Race class :
public class Race
{
public string RaceName { set; get; }
public Circuit Circuit { set; get; }
public string Date;
public string Time;
public StepDateTime FirstPractice { set; get; }
public StepDateTime SecondPractice { set; get; }
public StepDateTime ThirdPractice { set; get; }
public StepDateTime Qualifying { set; get; }
public StepDateTime Sprint { set; get; }
}
My Circuit class :
public class Circuit
{
public string CircuitName { set; get; }
public Location Location { set; get; }
}
My Location class :
public class Location
{
public double lat { set; get; }
[XmlAttribute("long")]
public double Long { set; get; }
public string Locality { set; get; }
public string Country { set; get; }
}
My StepDateTime class :
public class StepDateTime
{
public string Date { set; get; }
public string Time { set; get; }
}
I tried to make a text file where I deleted the first 2 lines of the XML answer hoping it would fix the issue but it didn't.
PS : I know I'm not using propreties and I am not respecting the c# conventions for the classes but it is temporary just so I can find a way to fix the issue. It is also my first time working with xml answers so I might have done some obvious errors.
Thanks for the help.