1

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);
}
6
  • The issue is surely that MySqlParameter can't be serialised, not MyClass - so why are you adding a constructor to MyClass? And what version of MySql are you using? Because MySqlParameter does have a parameterless constructor. Can you post a compilable reproduction of your problem? Commented Feb 20, 2020 at 8:45
  • Why would it be trying to serialise MySqlParameter? That's only the data being sent to the db when saving, it's in the parent method, not with the XmlSerializer Commented Feb 20, 2020 at 9:13
  • are you tried to change your constructor to private or internal? Commented Feb 20, 2020 at 9:26
  • Yes, tried all three Commented Feb 20, 2020 at 9:28
  • Why would it be trying to serialise - I don't know why, but the error message clearly states that it is being serialised. You will need to post some code. Commented Feb 20, 2020 at 9:29

2 Answers 2

1

You should have added

public MyClass(){}

to the class. There is a difference between no body and an empty body which is what you want on this constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

From my question: so I add a body and the first error comes back - I already tried that
as @Matthew asked, please post the code for the class you're trying to serialize, it does look like you trying to serialize a none searilizable object
1

I figured it out...

I had been creating a new XmlSerializer and passing in a parameter as posted in a SO answer:

XmlSerializer xmlSer = new XmlSerializer(this.GetType());

When really it works perfectly fine to use the same XmlSerializer as the deserialization:

XmlSerializer objSer = new XmlSerializer(typeof(MyClass));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.