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

4
  • The following may be helpful: stackoverflow.com/questions/68215415/… and stackoverflow.com/questions/68605811/… Commented Jan 31, 2022 at 20:48
  • 1
    The Net library defaults to having two levels of tags when serializing an array <parent><child></child><child></child><child></child><child></child></parent>. 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. Commented Feb 1, 2022 at 10:57
  • Per the docs: '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. Commented Feb 2, 2022 at 10:48
  • @CharlesMager Ah that's what I was doing wrong. Thanks! Commented Feb 2, 2022 at 16:47

0

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.