序列化时如何解决“没有无参数构造函数无法序列化对象”的错误

问题描述 投票:0回答:1

我在将对象导出到xml文件时遇到问题。我需要使用xml模式对文件进行序列化以使其更容易导入,但是我可以对类进行序列化,因为抛出了异常“如果没有无参数构造函数就无法序列化类,或者如果我使用特殊生成的xmlhelper,则抛出不能将对象TechnologyTable投射到TechTables.Common.TechnologyTable(这是另一个带有参数的类)

这里是导出功能

        private void _ExportTechTables(object SelectedItmes)
         {
     var Tables = (SelectedItmes as IList).Cast<TableListInfo>().ToList();  
        var Export = new SaveFileDialog();
        var serializer = new XmlSerializer(typeof(TechnologyTable));
        Export.Filter = "xml files |*.xml";
        Export.Title = "Export table to xml file";
        DateTime time = DateTime.UtcNow.ToLocalTime();
        Export.FileName = "Exported Tables: " + time;
        if(Export.ShowDialog() == DialogResult.OK)
        {
            using (Stream open = File.Open(Export.FileName, FileMode.OpenOrCreate))
            {
                foreach (var item in Tables)
                {
                    TechTables.Common.TechnologyTable Items = Plc.Instance.TableHandler.GetTable(item.Id, false, false);
                        serializer.Serialize(open, Items);
                }  
            };
        }
    }

这里是TechTables.Common.TechnologyTable

  public TechnologyTable(int id, string name, Material material, Thickness thickness,
        string lens, double focalLength, double nozzleDistance,
        bool gasStabilization, bool sensitivitySensor, double sensitivityArea,
        bool gasFlushingOn, int gasFlushingTime, double gasFlushingPressure, bool gasFeedbackOn, int gasFeedbackTime,
        double gasFeedbackPressure, double gasMaxPressure, bool gasContinuationOn,
        int gasContinuationTime, bool isTimeCountEnable, int maxTimeCount, bool isPiercingCountEnable, int maxPiercingCount, bool isColisionCountEnable, int maxColisionCount, Nozzle nozz, bool isTemplate = false, TechnologyTable template = null)
    {
        Id = id;
        IsTimeCountEnable = isTimeCountEnable;
        MaxTimeCount = maxTimeCount;
        IsPiercingCountEnable = isPiercingCountEnable;
        MaxPiercingCount = maxPiercingCount;
        IsColisionCountEnable = isColisionCountEnable;
        MaxColisionCount = maxColisionCount;
        Name = name;
        Nozzle = nozz;
        Material = material;
        Thickness = thickness;
        _cuttings = Cuttings;
        _piercings = Piercings;
        Lens = lens;
        FocalLength = focalLength;
        NozzleDistance = nozzleDistance;
        GasContinuationOn = gasContinuationOn;
        GasContinuationTime = gasContinuationTime;
        GasFeedbackOn = gasFeedbackOn;
        GasFeedbackPressure = gasFeedbackPressure;
        GasFeedbackTime = gasFeedbackTime;
        GasFlushingOn = gasFlushingOn;
        GasFlushingTime = gasFlushingTime;
        GasFlushingPressure = gasFlushingPressure;
        GasMaxPressure = gasMaxPressure;
        GasStabilization = gasStabilization;
        SensitivityArea = sensitivityArea;
        SensitivitySensor = sensitivitySensor;
        IsTemplate = isTemplate;
        Template = template;
    } #lower property but it's too much to paste them here 

这是我的SerializerHelper

  namespace Eagle.eSoft.Eris.ViewModel.ServiceOptions
 {
[XmlRoot(ElementName = "material", Namespace = 
 "http://tempuri.org/XMLSchema.xsd")]
public class Material
{
         [XmlElement(ElementName = "Id", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Id { get; set; }
    [XmlElement(ElementName = "Name", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Name { get; set; }
    [XmlElement(ElementName = "FullName", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string FullName { get; set; }
    [XmlElement(ElementName = "Code", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Code { get; set; }
}

    [XmlRoot(ElementName = "thickness", Namespace = "http://tempuri.org/XMLSchema.xsd")]
public class Thickness
{
    [XmlElement(ElementName = "thicknessId", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string ThicknessId { get; set; }
    [XmlElement(ElementName = "thicknessValue", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string ThicknessValue { get; set; }
    [XmlElement(ElementName = "unit", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Unit { get; set; }
}

[XmlRoot(ElementName = "nozz", Namespace = "http://tempuri.org/XMLSchema.xsd")]
public class Nozz
{
    [XmlElement(ElementName = "NozzleID", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string NozzleID { get; set; }
    [XmlElement(ElementName = "NozzleName", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string NozzleName { get; set; }
    [XmlElement(ElementName = "SocketType", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string SocketType { get; set; }
}

[XmlRoot(ElementName = "TechnologyTable", Namespace = "http://tempuri.org/XMLSchema.xsd")]
public class TechnologyTable
{
    [XmlElement(ElementName = "Id", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Id { get; set; }
    [XmlElement(ElementName = "Name", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Name { get; set; }
    [XmlElement(ElementName = "material", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public Material Material { get; set; }
    [XmlElement(ElementName = "thickness", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public Thickness Thickness { get; set; }
    [XmlElement(ElementName = "lens", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Lens { get; set; }
    [XmlElement(ElementName = "focalLenght", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string FocalLenght { get; set; }
    [XmlElement(ElementName = "nozzleDistance", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string NozzleDistance { get; set; }
    [XmlElement(ElementName = "gasStabilization", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasStabilization { get; set; }
    [XmlElement(ElementName = "sensitivitySensor", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string SensitivitySensor { get; set; }
    [XmlElement(ElementName = "sensitivityArea", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string SensitivityArea { get; set; }
    [XmlElement(ElementName = "gasFlushingOn", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFlushingOn { get; set; }
    [XmlElement(ElementName = "gasFlushingTime", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFlushingTime { get; set; }
    [XmlElement(ElementName = "gasFlushingPressure", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFlushingPressure { get; set; }
    [XmlElement(ElementName = "gasFeedbackOn", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFeedbackOn { get; set; }
    [XmlElement(ElementName = "gasFeedbackTime", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFeedbackTime { get; set; }
    [XmlElement(ElementName = "gasFeedBackPressure", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasFeedBackPressure { get; set; }
    [XmlElement(ElementName = "gasMaxPressure", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasMaxPressure { get; set; }
    [XmlElement(ElementName = "gasContinuationOn", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasContinuationOn { get; set; }
    [XmlElement(ElementName = "gasContinuationTime", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string GasContinuationTime { get; set; }
    [XmlElement(ElementName = "isTimeCountEnable", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string IsTimeCountEnable { get; set; }
    [XmlElement(ElementName = "maxTimeCount", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string MaxTimeCount { get; set; }
    [XmlElement(ElementName = "isPiercingCountEnable", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string IsPiercingCountEnable { get; set; }
    [XmlElement(ElementName = "maxPiercingCount", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string MaxPiercingCount { get; set; }
    [XmlElement(ElementName = "isColisionCountEnable", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string IsColisionCountEnable { get; set; }
    [XmlElement(ElementName = "maxColisionCount", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string MaxColisionCount { get; set; }
    [XmlElement(ElementName = "nozz", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public Nozz Nozz { get; set; }
    [XmlElement(ElementName = "isTemplate", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string IsTemplate { get; set; }
    [XmlElement(ElementName = "Template", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public string Template { get; set; }
}

[XmlRoot(ElementName = "TechnologyTableList", Namespace = "http://tempuri.org/XMLSchema.xsd")]
public class TechnologyTableSerializer
{
    [XmlElement(ElementName = "TechnologyTable", Namespace = "http://tempuri.org/XMLSchema.xsd")]
    public List<TechnologyTable> TechnologyTable { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}

}

c# xml xml-serialization
1个回答
0
投票

正如错误消息所言,您需要一个无参数的构造函数。

public TechnologyTable()
{}

所以这样。这是因为序列化程序使用该无参数构造函数创建该类的空实例,然后设置所有公共属性。

© www.soinside.com 2019 - 2024. All rights reserved.