Method for C to realize deserialization of xml file and read data to object

  • 2021-07-10 20:33:26
  • OfStack

This article describes the example of C # implementation of xml file deserialization read-in data to object method. Share it for your reference. The specific implementation method is as follows:

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);

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);

I hope this article is helpful to everyone's C # programming.

Related articles: