【发布时间】:2019-10-07 10:22:14
【问题描述】:
我在将对象导出到 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; }
}
}
【问题讨论】:
-
你只需要第二个构造函数:public TechnologyTable() {}
标签: c# xml xml-serialization