XmlSerializer抛出InvalidOperationException

时间:2014-04-04 11:08:01

标签: c# xml serialization xsd

InterfaceType interfaceType = new InterfaceType() { definitions = new UnitType[0][] };
try
{
    interfaceType = fileManager.deserialize<InterfaceType>("C:\\Users\\Yogev\\Desktop\\namer_NMC_TILTAN_Interface.xml");
}
catch (Exception ex)
{
    Debug.WriteLine("Error deserialize" + ex.ToString());
}

这是类的创建和deserialize方法调用。

我正在从xsd.exe反序列化自动生成的(部分)类。这是班级:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/IRSSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute("interface", Namespace="http://tempuri.org/IRSSchema.xsd", IsNullable=false)]
public partial class InterfaceType {

private System.Xml.XmlNode[] descriptionField;

private UnitType[][] definitionsField;

private IncludeType[] includeField;

private GroupType[] groupField;

private string nameField;

private string formatField;

private bool isBigEndianField;

private bool isBigEndianFieldSpecified;

我得到了一个例外:

error CS0030: Cannot convert type 'UnitType[]' to 'UnitType'
error CS0029: Cannot implicitly convert type 'UnitType' to 'UnitType[]'

1 个答案:

答案 0 :(得分:-1)

我试图序列化你的类,但实际上遇到了同样的问题。我试图序列化一维数组,并且它有效。所以我建议你将二维数组包装成一个容器数组。然后它会工作;)

尝试向数组添加属性,例如:

[XmlArray("groupField")]
[XmlArrayItem("GroupType", typeof(GroupType))]
public GroupType[] groupField { get; set; }