0

I'm currently trying to copy (by use of code) a .xml file and have approached it many different ways. This way however seems to be the one I get the most as i have I've it before but to create a console.writeline output. I can't seem to get it to work for a console.out when producing the .xml layout. It gives me the following error

"cannot be serialized because it does not have a parameterless constructor"

please could anyone help me with this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
class Program
{
    const string FILENAME = @"SmallResults.xml";
    static void Main(string[] args)
    {
        List<BasicEvent> NewBasicEvent = new List<BasicEvent>();
        XmlTextReader fileReader = new XmlTextReader("SmallResults.xml");
        XmlDocument xml = new XmlDocument();
        xml.Load(fileReader);
        XmlNodeList BasicEvents = xml.SelectNodes("/HiP-HOPS_Results/FaultTrees/FMEA/Component/Events/BasicEvent");
        if (BasicEvents != null)
        {
            XmlNode[] nodearray = BasicEvents.Cast<XmlNode>().ToArray();
            for (int i = 0; i < BasicEvents.Count; i++)
            {
                NewBasicEvent.Add(new BasicEvent(nodearray[i].FirstChild.NextSibling.InnerText, nodearray[i].FirstChild.NextSibling.NextSibling.InnerText, nodearray[i].FirstChild.NextSibling.NextSibling.InnerText, nodearray[i].FirstChild.NextSibling.NextSibling.NextSibling.InnerText));
                new XmlSerializer(typeof(BasicEvent)).Serialize(Console.Out, nodearray );
            }
        }


    }
}
public class FMEA
{
    public List<Component> components { get; set; }
}
public class Component
{
    public string CompName { get; set; }
    public string BasicEventID { get; set; }
    public List<BasicEvent> basicevents { get; set; }
}
public class Events
{
}
public class BasicEvent
{
    public string BasicEventName { get; set; }
    public string BasicEventShortName { get; set; }
    public string BasicEventDescription { get; set; }
    public string BasicEventUnavalability { get; set; }
    public List<Effects> effects { get; set; }

    public BasicEvent( string basiceventname, string basiceventshortname, string basiceventdescription, string basiceventunavalability )
    {
        BasicEventName = basiceventname;
        BasicEventShortName = basiceventshortname;
        BasicEventDescription = basiceventdescription;
        BasicEventUnavalability = basiceventdescription;
    }
Michael Armes
  • 1,056
  • 2
  • 17
  • 31
K---
  • 11
  • 9
  • 1
    As the error says there is no default parameterless constructor available. Deserialization needs that to exist so the object can be created and then the values set. Add one to the class. – Sami Kuhmonen Dec 06 '16 at 18:49

1 Answers1

4

The message in the exception is quite clear!

Add the default constructor then:

public BasicEvent( ){}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97