使用XmlSerializer时无参数构造函数

时间:2016-12-06 18:47:58

标签: c# xml

我目前正在尝试复制(通过使用代码)一个.xml文件,并以不同的方式接近它。然而,这种方式似乎是我得到的最多,因为我之前已经拥有它,但创建了一个console.writeline输出。在生成.xml布局时,我似乎无法让它在console.out中工作。它给我以下错误

  

“无法序列化,因为它没有无参数构造函数”

请任何人帮助我。

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;
    }

1 个答案:

答案 0 :(得分:4)

异常中的消息非常清楚!

然后添加默认构造函数:

public BasicEvent( ){}