Is it possible to deserialize xml with mutiple xml root?
If not, what is the best way to bind elements to a single model if it has the same properties on it?
Here are my codes:
[XmlRoot(ElementName = "residential")]
[XmlRoot(ElementName = "commercial")]
public class Property
{
[XmlElement(ElementName = "name")]
public string Name{ get; set; }
[XmlElement(ElementName = "description")]
public string Description { get; set; }
}
[XmlRoot(ElementName = "propertyList")]
public class PropertyList
{
[XmlChoiceIdentifier("EnumType")]
[XmlElement(ElementName = "residential")]
[XmlElement(ElementName = "commercial")]
[XmlElement(ElementName = "land")]
[XmlElement(ElementName = "rental")]
[XmlElement(ElementName = "holidayRental")]
[XmlElement(ElementName = "rural")]
public List<Property> Properties { get; set; }
[XmlIgnore]
public PropertyType[] EnumType;
[XmlAttribute(AttributeName = "date")]
public string Date { get; set; }
[XmlAttribute(AttributeName = "username")]
public string Username { get; set; }
[XmlAttribute(AttributeName = "password")]
public string Password { get; set; }
[XmlIgnore]
public string Type { get; set; }
}
[XmlType(IncludeInSchema = false)]
public enum PropertyType
{
residential,
commercial,
land,
rental,
holidayRental,
rural
}
var serializer = new XmlSerializer(typeof(Business.Models.PropertyList), new XmlRootAttribute("propertyList"));
I'm getting Object reference not set to an instance of an object error when trying to deserialize the XML.
Here's the sample xml
<propertyList date="2020-09-09T09:35:38" username="test" password="test">
<commercial modTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</commercial>
<commercial modTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</commercial>
<commercial modTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</commercial>
<residentialmodTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</residential>
<residentialmodTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</residential>
<commercial modTime="2020-09-09T09:35:38" status="current">
<agentID>12345</agentID>
<uniqueID>12345</uniqueID>
</commercial>
</propertylist