鉴于下面的类定义,有人可以解释为什么我在调用Serialize()方法时会遇到此异常.
{“有一个错误反映了’FNM.MISMO_3_0.FOREIGN_OBJECT’类型.”}
{“选择标识符’ItemsElementname’的类型与’Items’的类型不一致.请使用System.Collections.Generic.List`1 [[MISMO_3_0.ItemsChoiceType,MISMO_3_0,Version = 1.0.0.0,Culture = neutral,PublicKeyToken]的数组= NulL]].“}
谢谢,
斯蒂芬
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml","4.0.30319.225")][System.SerializableAttribute()][System.Diagnostics.DeBUGgerStepThroughAttribute()][System.ComponentModel.DesignercategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(namespace = "http://www.mismo.org/resIDential/2009/schemas")][System.Xml.Serialization.XmlRootAttribute(namespace = "http://www.mismo.org/resIDential/2009/schemas",IsNullable = true)]public partial class FOREIGN_OBJECT{ private List<object> itemsFIEld; private List<ItemsChoiceType> itemsElementnameFIEld; private MISMOresourcelink typeFIEld; private bool typeFIEldSpecifIEd; private string roleFIEld; private string TitleFIEld; private string labelFIEld; private string sequenceNumberFIEld; private static System.Xml.Serialization.XmlSerializer serializer; public FOREIGN_OBJECT() { this.itemsElementnameFIEld = new List<ItemsChoiceType>(); this.itemsFIEld = new List<object>(); } [System.Xml.Serialization.XmlElementAttribute("CharacterEnCodingSetType",typeof(CharacterEnCodingSetEnum),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("CharacterEnCodingSetTypeOtherDescription",typeof(MISMOString),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("EXTENSION",typeof(EXTENSION),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("EmbeddedContentXML",typeof(MISMOXML),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("MIMETypeIDentifIEr",typeof(MISMOIDentifIEr),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("ObjectCreatedDatetime",typeof(MISMODatetime),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("ObjectDescription",Order = 0)] [System.Xml.Serialization.XmlElementAttribute("ObjectEnCodingType",typeof(ObjectEnCodingEnum),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("ObjectEnCodingTypeOtherDescription",Order = 0)] [System.Xml.Serialization.XmlElementAttribute("Objectname",Order = 0)] [System.Xml.Serialization.XmlElementAttribute("ObjectURI",typeof(string),DataType = "anyURI",Order = 0)] [System.Xml.Serialization.XmlElementAttribute("OriginalCreatorDigestValue",typeof(MISMovalue),Order = 0)] [System.Xml.Serialization.XmlElementAttribute("REFERENCE",typeof(REFERENCE),Order = 0)] [System.Xml.Serialization.XmlChoiceIDentifIErAttribute("ItemsElementname")] public List<object> Items { get { return this.itemsFIEld; } set { this.itemsFIEld = value; } } [System.Xml.Serialization.XmlElementAttribute("ItemsElementname",Order = 1)] [System.Xml.Serialization.XmlignoreAttribute()] public List<ItemsChoiceType> ItemsElementname { get { return this.itemsElementnameFIEld; } set { this.itemsElementnameFIEld = value; } } [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.QualifIEd,namespace = "http://www.w3.org/1999/xlink")] public MISMOresourcelink type { get { return this.typeFIEld; } set { this.typeFIEld = value; } } [System.Xml.Serialization.XmlignoreAttribute()] public bool typespecifIEd { get { return this.typeFIEldSpecifIEd; } set { this.typeFIEldSpecifIEd = value; } } [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.QualifIEd,namespace = "http://www.w3.org/1999/xlink")] public string role { get { return this.roleFIEld; } set { this.roleFIEld = value; } } [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.QualifIEd,namespace = "http://www.w3.org/1999/xlink")] public string Title { get { return this.TitleFIEld; } set { this.TitleFIEld = value; } } [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.QualifIEd,namespace = "http://www.w3.org/1999/xlink",DataType = "NMTOKEN")] public string label { get { return this.labelFIEld; } set { this.labelFIEld = value; } } [System.Xml.Serialization.XmlAttributeAttribute(DataType = "integer")] public string SequenceNumber { get { return this.sequenceNumberFIEld; } set { this.sequenceNumberFIEld = value; } } private static System.Xml.Serialization.XmlSerializer Serializer { get { if ((serializer == null)) { serializer = new System.Xml.Serialization.XmlSerializer(typeof(FOREIGN_OBJECT)); } return serializer; } } #region Serialize/Deserialize /// <summary> /// Serializes current FOREIGN_OBJECT object into an XML document /// </summary> /// <returns>string XML value</returns> public virtual string Serialize(System.Text.EnCoding enCoding) { System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try { memoryStream = new System.IO.MemoryStream(); System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings(); xmlWriterSettings.EnCoding = enCoding; System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream,xmlWriterSettings); Serializer.Serialize(xmlWriter,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally { if ((streamReader != null)) { streamReader.dispose(); } if ((memoryStream != null)) { memoryStream.dispose(); } } } public virtual string Serialize() { return Serialize(EnCoding.UTF8); } /// <summary> /// Deserializes workflow markup into an FOREIGN_OBJECT object /// </summary> /// <param name="xml">string workflow markup to deserialize</param> /// <param name="obj">Output FOREIGN_OBJECT object</param> /// <param name="exception">output Exception value if deserialize Failed</param> /// <returns>true if this XmlSerializer can deserialize the object; otherwise,false</returns> public static bool Deserialize(string xml,out FOREIGN_OBJECT obj,out System.Exception exception) { exception = null; obj = default(FOREIGN_OBJECT); try { obj = Deserialize(xml); return true; } catch (System.Exception ex) { exception = ex; return false; } } public static bool Deserialize(string xml,out FOREIGN_OBJECT obj) { System.Exception exception = null; return Deserialize(xml,out obj,out exception); } public static FOREIGN_OBJECT Deserialize(string xml) { System.IO.StringReader stringReader = null; try { stringReader = new System.IO.StringReader(xml); return ((FOREIGN_OBJECT)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally { if ((stringReader != null)) { stringReader.dispose(); } } } /// <summary> /// Serializes current FOREIGN_OBJECT object into file /// </summary> /// <param name="filename">full path of outupt xml file</param> /// <param name="exception">output Exception value if Failed</param> /// <returns>true if can serialize and save into file; otherwise,false</returns> public virtual bool Savetofile(string filename,System.Text.EnCoding enCoding,out System.Exception exception) { exception = null; try { Savetofile(filename,enCoding); return true; } catch (System.Exception e) { exception = e; return false; } } public virtual bool Savetofile(string filename,out System.Exception exception) { return Savetofile(filename,EnCoding.UTF8,out exception); } public virtual voID Savetofile(string filename) { Savetofile(filename,EnCoding.UTF8); } public virtual voID Savetofile(string filename,System.Text.EnCoding enCoding) { System.IO.StreamWriter streamWriter = null; try { string xmlString = Serialize(enCoding); streamWriter = new System.IO.StreamWriter(filename,false,EnCoding.UTF8); streamWriter.Writeline(xmlString); streamWriter.Close(); } finally { if ((streamWriter != null)) { streamWriter.dispose(); } } } /// <summary> /// Deserializes xml markup from file into an FOREIGN_OBJECT object /// </summary> /// <param name="filename">string xml file to load and deserialize</param> /// <param name="obj">Output FOREIGN_OBJECT object</param> /// <param name="exception">output Exception value if deserialize Failed</param> /// <returns>true if this XmlSerializer can deserialize the object; otherwise,false</returns> public static bool LoadFromfile(string filename,out System.Exception exception) { exception = null; obj = default(FOREIGN_OBJECT); try { obj = LoadFromfile(filename,enCoding); return true; } catch (System.Exception ex) { exception = ex; return false; } } public static bool LoadFromfile(string filename,out System.Exception exception) { return LoadFromfile(filename,out exception); } public static bool LoadFromfile(string filename,out FOREIGN_OBJECT obj) { System.Exception exception = null; return LoadFromfile(filename,out exception); } public static FOREIGN_OBJECT LoadFromfile(string filename) { return LoadFromfile(filename,EnCoding.UTF8); } public static FOREIGN_OBJECT LoadFromfile(string filename,System.Text.EnCoding enCoding) { System.IO.fileStream file = null; System.IO.StreamReader sr = null; try { file = new System.IO.fileStream(filename,fileMode.Open,fileAccess.Read); sr = new System.IO.StreamReader(file,enCoding); string xmlString = sr.ReadToEnd(); sr.Close(); file.Close(); return Deserialize(xmlString); } finally { if ((file != null)) { file.dispose(); } if ((sr != null)) { sr.dispose(); } } } #endregion #region Clone method /// <summary> /// Create a clone of this FOREIGN_OBJECT object /// </summary> public virtual FOREIGN_OBJECT Clone() { return ((FOREIGN_OBJECT)(this.MemberwiseClone())); } #endregion}解决方法 仅当ChoiceIDentifIEr是一个数组而不是泛型列表时,才支持XmlChoiceIDentifIErAttribute中的多个元素.
[System.Xml.Serialization.XmlElementAttribute("ItemsElementname",Order = 1)][System.Xml.Serialization.XmlignoreAttribute()]public ItemsChoiceType[] ItemsElementname{ // ...}
该错误令人困惑,因为它假设Items是类似数组(A列表)并且您的选择标识符不是数组,您在选择标识符的类型之前错过了数组限定符.但事实并非如此,所以这个建议完全是误导性的.
注意:理论上看代码(来自微软源服务器)项不需要是数组……实际上,临时序列化类的生成完全爆炸,因为List.Length不是公共的(它是显式实现的)
总结以上是内存溢出为你收集整理的c# – .NET XML序列化错误(反映类型有错误)全部内容,希望文章能够帮你解决c# – .NET XML序列化错误(反映类型有错误)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)