I'm trying to serialize an object to xml using XmlSerializer
but keep getting this error:
System.InvalidOperationException: [MySql.Data.MySqlClient.MySqlParameter] cannot be serialized because it does not have a parameterless constructor.
I followed the solution to why xml serializable class need a parameterless constructor which seems to have solved the problem for everyone else, but I still get an error. When I add public MyClass()
it gives me 'MyClass.MyClass()' must declare a body because it is not marked abstract, extern, or partial
, so I add a body and the first error comes back.
Note that originally there aren't any constructors, parameterless or otherwise, so I don't understand why it isn't just automatically creating its own.
What am I doing wrong?
Code added:
MyFile.cs
public async Task Save(MyClass xmlObj)
{
XmlSerializer xmlSer = new XmlSerializer(this.GetType()); //I think the problem is likely to be here
StringWriter strWriter = new StringWriter();
xmlSer.Serialize(strWriter, xmlObj);
objToUpdate.ColName = (strWriter.ToString());
await dbContext.SaveChangesAsync();
}
File.cs
public async Task<IActionResult> SaveData(params...)
{
await new MyFile<MySqlParameter>(accessParam, dbContext).Save(xmlObj);
}