使用序列化从XML文件读入C#类theIdTheIDtheIDtheNameNewName下面是我正在使用的代码,虽然它执行没有错误,但是没有数据被读入类PropertiesMapping,我哪里错了?属性映射pm=null;尝试{System.IO.StreamReaderstr=newSystem.IO.StreamReader(@"PropertyMapping.xml");System.Xml.Serialization.XmlSerializerxSerializer=newSystem.Xml.Serialization.XmlSerializer(typeof(PropertiesMapping));pm=(PropertiesMapping)xSerializer.Deserialize(str);海峡关闭();}catch(Exceptionex){Console.WriteLine(ex.ToString());}[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,Namespace="")][System.Xml.Serialization.XmlRootAttribute(Namespace="",IsNullable=false)]publicclassPropertiesMapping{privatestringm_WEB_Class="";私有字符串m_COM_Class="";私有列表m_EachProperty=null;publicstringWEB_Class{get{returnm_WEB_Class;}设置{m_WEB_Class=值;}}公共字符串CO}设置{m_COM_Class=值;}}publicIndividualPropertiesGetIndividualProperties(intiIndex){returnm_EachProperty[iIndex];}publicvoidSetIndividualProperties(IndividualPropertiestheProp){m_EachProperty.Add(the}PropIndividualProperties{privatestringm_WEB_PropertyField;privatestringm_COM_PropertyField;publicstringWEB_Property{get{returnthis.m_WEB_PropertyField;}set{this.m_WEB_PropertyField=value;}}publicstringCOM_Property{get{returnthis.m_COM_PropertyField;}set{this.m_COM_PropertyField=value;}}}您可以使用XmlElementAttribute来简化C#命名,以在XmlSerializer序列化或反序列化对象时指示公共字段或属性表示XML元素包含它。您需要将XmlArrayItemAttribute用于Mappings元素。表示指定派生类型的属性,XmlSerializer可以将其放置在序列化数组中。类:[XmlType("PropertiesMapping")]publicclassPropertyMapping{publicPropertyMapping(){Properties=newList();}[XmlElement("Property")]publicListProperties{get;放;}}publicclassProperty{publicProperty(){Mappings=newList();}[XmlElement("WEB_Class")]publicstringWebClass{get;放;}[XmlElement("COM_Class")]publicstringComClass{get;放;}[XmlArray("Mappings")][XmlArrayItem("Map")]publicListMappings{get;放;}}[XmlType("Map")]publicclassMapping{[XmlElement("WEB_Property")]publicstringWebProperty{get;放;}[XmlElement("COM_Property")]publicstringComProperty{get;放;}}演示:PropertyMapping结果;varserializer=newXmlSerializer(typeof(PropertyMapping));使用(varstream=newStringReader(数据))使用(varreader=XmlReader.Create(stream)){结果=(PropertyMapping)serializer.Deserialize(reader);您不需要显式声明该属性。只要确保名称匹配即可。唯一的非默认部分是[XmlArrayItem("Map")]属性,您需要为地图数组使用不同的名称。但是,您可以通过指定XmlElementAttribute使用与COM_Property和WEB_Property不同的名称。classProgram{staticvoidMain(string[]args){stringtestData=@"InfoRequestCInfoReqtheIdTheIDtheNameNewName";varsr=newSystem.IO.StringReader(testData);varxs=newXmlSerializer(typeof(PropertiesMapping));对象结果=xs.Deserialize(sr);}}[可序列化]publicclassPropertiesMapping{publicPropertyProperty{get;放;}}[Serializable]publicclassProperty{[XmlElement("WEB_Class")]publicstringWebClass{get;放;}[XmlElement("COM_Class")]publicstringComClass{get;放;}[XmlArrayItem("Map")]publicMapping[]Mappings{get;放;}}[Serializable]publicclassMapping{[XmlElement("WEB_Property")]publicstringWebProperty{get;放;}[XmlElement("COM_Property")]publicstringComProperty{get;放;根据您要实现的目标,使用XDocument和Linq-to-XML可以更轻松地完成。至于序列化,这里有一个类结构:以上是C#学习教程:使用序列化将XML文件读入C#类分享所有内容,如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicclassPropertiesMapping{[XmleElement]publicstringWEB_Class{get;set;}[XmleElement]publicstringCOM_Class{get;set;}[XmlArray("Mappings")]publicMap[]Mappings{get;set;}}publicclassMap{[XmleElement]publicstringWEB_Property{get;set;}[XmleElement]publicstringCOM_Property{get;set;}}本文收集自网络,不代表位置。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
