Skip to content

Instantly share code, notes, and snippets.

@mgravell
Created June 23, 2021 10:56
Show Gist options
  • Save mgravell/0a1d3a1de0cd9ab0614b375dc5425bb8 to your computer and use it in GitHub Desktop.
Save mgravell/0a1d3a1de0cd9ab0614b375dc5425bb8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class P
{
static void Main()
{
var xml = @"<Response><carList>
<numberOfRecords>237</numberOfRecords>
<year>1982</year>
<year>1983</year>
<year>1978</year>
<year>1955</year>
<year>1998</year>
</carList></Response>";
var ser = new XmlSerializer(typeof(Response));
var response = (Response)ser.Deserialize(new StringReader(xml));
var carList = response.CarList;
Console.WriteLine(carList.NumberOfRecords);
if (carList.Year is not null)
{
foreach (var year in carList.Year)
{
Console.WriteLine(year);
}
}
}
}
public class Response
{
[XmlElement("carList")]
public CarList? CarList { get; set; }
}
public class CarList
{
[XmlElement("numberOfRecords")]
public int? NumberOfRecords { get; set; }
[XmlElement("year")]
public List<string>? Year { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment