Here's how you can convert your XML string to C# classes, we will be using the converter and built in libraries like 'System.Xml.Serialization' to parse our object.
The XML string should be correctly formatted before converting it to C# classes.
Here's an example of an XML string:
You can optionally choose from the settings to:
When you copy the returned classes in the directory of your solution, you can deserialize your XML string or file using the 'Root' class as mentioned in commented example below:
Here are the classes returned from the previous example:
/*
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(Realestates));
using (StringReader reader = new StringReader(xml))
{
var test = (Realestates)serializer.Deserialize(reader);
}
*/
[XmlRoot(ElementName="additionalCosts")]
public class AdditionalCosts
{
[XmlElement(ElementName = "value")]
public string Value { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
[XmlElement(ElementName = "marketingType")]
public string MarketingType { get; set; }
[XmlElement(ElementName = "priceIntervalType")]
public string PriceIntervalType { get; set; }
}
[XmlRoot(ElementName = "realestates")]
public class Realestates
{
[XmlElement(ElementName = "externalId")]
public string ExternalId { get; set; }
[XmlElement(ElementName = "title")]
public string Title { get; set; }
[XmlElement(ElementName = "creationDate")]
public string CreationDate { get; set; }
[XmlElement(ElementName = "lastModificationDate")]
public string LastModificationDate { get; set; }
[XmlElement(ElementName = "thermalCharacteristic")]
public string ThermalCharacteristic { get; set; }
[XmlElement(ElementName = "energyConsumptionContainsWarmWater")]
public string EnergyConsumptionContainsWarmWater { get; set; }
[XmlElement(ElementName = "buildingEnergyRatingType")]
public string BuildingEnergyRatingType { get; set; }
[XmlElement(ElementName = "additionalArea")]
public string AdditionalArea { get; set; }
[XmlElement(ElementName = "numberOfFloors")]
public string NumberOfFloors { get; set; }
[XmlElement(ElementName = "additionalCosts")]
public AdditionalCosts AdditionalCosts { get; set; }
}
This is how can you deserialize your XML string in your C# code:
XmlSerializer serializer = new XmlSerializer(typeof(Realestates));
using (StringReader reader = new StringReader(xml))
{
var test = (Realestates)serializer.Deserialize(reader);
}
And this is how can you deserialize your XML file in your C# code:
XmlSerializer serializer = new XmlSerializer(typeof(Realestates));
using (StreamReader reader = new StreamReader(pathOfYourXMLFile))
{
var test = (Realestates)serializer.Deserialize(reader);
}