I have an interface IInput
that is preventing the XmlSerializer from serializing the class natively (because it doesnt like interfaces). I found a hack/workaround that attempts to just create the underlying implementation when deserializing and then casts that back to the interface. The deserializer knows the underlying implementation because its encoded as a attribute AssemblyQualifiedName
In order to take advantage of this technique I have to implement IXmlSerializable, but only 1 property really needs help (IInput Input), I wish for all the other ones to act as if they were normal. Here is my class, it works as expected but seems like a very messy way of getting types of which the normal XMLserializer can serialize to conform to an IXmlSerialiable interface.
Is there some sort of "Serialize all properties natively except x"? If not what is one way I can make this more readable and/or less copy and pasted
public class JobInput : IJobInput, IXmlSerializable
{
public int AgencyId { get; set; }
public Guid ExternalId { get; set; }
public string Requester { get; set; }
public IInput Input { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
reader.MoveToContent();
reader.ReadStartElement();
if (!reader.IsEmptyElement)
{
reader.ReadStartElement("AgencyId");
var xmlSerializer = new XmlSerializer(AgencyId.GetType());
AgencyId = ((int)xmlSerializer.Deserialize(reader));
reader.ReadEndElement();
reader.ReadStartElement("ExternalId");
xmlSerializer = new XmlSerializer(ExternalId.GetType());
ExternalId = ((Guid)xmlSerializer.Deserialize(reader));
reader.ReadEndElement();
reader.ReadStartElement("Requester");
xmlSerializer = new XmlSerializer(typeof(string));
Requester = ((string)xmlSerializer.Deserialize(reader));
reader.ReadEndElement();
var type = Type.GetType(reader.GetAttribute("AssemblyQualifiedName"), true);
reader.ReadStartElement("IInput");
xmlSerializer = new XmlSerializer(type);
Input = ((IInput)xmlSerializer.Deserialize(reader));
reader.ReadEndElement();
reader.ReadEndElement();
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("AgencyId");
var xmlSerializer = new XmlSerializer(AgencyId.GetType());
xmlSerializer.Serialize(writer, AgencyId);
writer.WriteEndElement();
writer.WriteStartElement("ExternalId");
xmlSerializer = new XmlSerializer(ExternalId.GetType());
xmlSerializer.Serialize(writer, ExternalId);
writer.WriteEndElement();
writer.WriteStartElement("Requester");
xmlSerializer = new XmlSerializer(Requester.GetType());
xmlSerializer.Serialize(writer, Requester);
writer.WriteEndElement();
writer.WriteStartElement("IInput");
writer.WriteAttributeString("AssemblyQualifiedName", Input.GetType().AssemblyQualifiedName);
xmlSerializer = new XmlSerializer(Input.GetType());
xmlSerializer.Serialize(writer, Input);
writer.WriteEndElement();
}
}
Is it possible to have a generic function that can just detect the type for all the concrete types and serialize/deserialize appropriately. I would like something like
public void WriteXml(XmlWriter writer) {
GenericSerialize("AgencyId", AgencyId, writer);
GenericSerialize("ExternalId", ExternalId, writer);
GenericSerialize("Requester", Requester, writer);
writer.WriteStartElement("IInput");
writer.WriteAttributeString("AssemblyQualifiedName", Input.GetType().AssemblyQualifiedName);
xmlSerializer = new XmlSerializer(Input.GetType());
xmlSerializer.Serialize(writer, Input);
writer.WriteEndElement();
}