0

I have a class that I want to serialise to XML:

public class Infinitive
{
    private string english;
    private string spanish;
    private string[] englishAccepted;
    private ConjugationModel model;

    public Infinitive()
    {
        //Parameterless constructor to allow the class to be serialised
    }
        
    public Infinitive(string english, string spanish) //Constructor if only 1 acceptable answer for English and regular conjugation
    {
        this.english = english;
        this.spanish = spanish;
        englishAccepted = new string[] { english };
        model = (ConjugationModel)RegularConjugation(spanish);
    }
    public Infinitive(string english, string spanish, string[] englishAccepted) //Constructor if multiple answers for English
    {
        this.english = english;
        this.spanish = spanish;
        if (englishAccepted[0] == "") //Handling for if englishAccepted is blank
        {
            this.englishAccepted = new string[] { english };
        }
        else
        {
            this.englishAccepted = englishAccepted;
        }
        model = (ConjugationModel)RegularConjugation(spanish);
    }
    public string English { get => english; }

    public string Spanish { get => spanish; }

    public ConjugationModel Model { get => model; }

    public string[] EnglishAccpeted { get => englishAccepted; }
    //There are also some methods in the class

And a fairly straightforward serialiser

public void Serialise<T>(T[] objects, string filename)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T[]));
    TextWriter writer = new StreamWriter(filename);
    serializer.Serialize(writer, objects);
    writer.Close();
}

However, when I input data and run the program, the file gets created and I end up with the following XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfInfinitive xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Infinitive />
  <Infinitive />
  <Infinitive />
  <Infinitive />
  <Infinitive />
  <Infinitive />
  <Infinitive />
  <Infinitive />
</ArrayOfInfinitive>

I have experimented with using XML serialisation tags, such as [Serializable] and [XmlElement] but nothing has changed the output so far.

What am I doing wrong?

Thanks in advance

  • The following may be helpful: https://stackoverflow.com/questions/68215415/invalidoperationexception-when-deserializing-custom-xml-lack-of-namespace/68232320#68232320 and https://stackoverflow.com/questions/68605811/xmlserializer-find-element-with-name-and-attribute-value/68607179#68607179 – Tu deschizi eu inchid Jan 31 '22 at 20:48
  • 1
    The Net library defaults to having two levels of tags when serializing an array . Adding [XmlElement] changes the defaults and only gives one level of tags. Most xml are "well formed" which means there is only one root tag (not an array). So to have a "well formed" xml you cannot have an array at the root an need the tag ArrayOfInfinitive. – jdweng Feb 01 '22 at 10:57
  • Per [the docs](https://learn.microsoft.com/en-us/dotnet/standard/serialization/introducing-xml-serialization): _'Only public properties and fields can be serialized. Properties must have public accessors (get and set methods)'_. You only have public `get` accessors. If you'd rather not add public setters, you might consider a DTO class for serialisation and map to that. – Charles Mager Feb 02 '22 at 10:48
  • @CharlesMager Ah that's what I was doing wrong. Thanks! – Samuel Glover Feb 02 '22 at 16:47

0 Answers0