本文实例讲述了C#实现xml文件反序列化读入数据到object的方法。分享给大家供大家参考。具体实现方法如下:
public static object DeSerializeFromXmlString(System.Type typeToDeserialize, string xmlString) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString); MemoryStream memoryStream = new MemoryStream(bytes); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeToDeserialize); return xmlSerializer.Deserialize(memoryStream);}Example[Test]public void GetBigList() { var textRepository = ObjectFactory.GetInstance<ITextRepository>(); List<BrandAndCode> brandAndCodeList = textRepository.GetList(...); string xml = SerializeToXmlString(brandAndCodeList); Console.Out.WriteLine("xml = {0}", xml); var brandAndCodeList2 = DeSerializeFromXmlString(typeof (BrandAndCode[]), xml);}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答