1#
单元测试(如何序列化导出至xml并读取反序列化)
  1. [TestClass]
  2.     public class UnitTest1
  3.     {
  4.         [TestMethod]
  5.         public void TestSerialize()
  6.         {
  7.             var banks = new Banks();
  8.             banks.BankInfo = new List<BankPayConfigInfo>();
  9.             banks.BankInfo.Add(new BankPayConfigInfo());

  10.             XmlSerialHelper.SerializeObject(@"D:\abc.xml", banks);

  11.             var banks2 = XmlSerialHelper.DeserializeObject<Banks>(@"D:\abc.xml");

  12.             var c = banks2.BankInfo.Count;

  13.             Assert.Inconclusive("success");
  14.         }
  15.     }
复制代码
引用Xml的帮助类
  1. public static class XmlSerialHelper
  2.     {
  3.         public static void SerializeObject<TConfig>(string filename, TConfig config)
  4.             where TConfig : class
  5.         {
  6.             var mySerializer = new XmlSerializer(typeof(TConfig));

  7.             var writer = new StreamWriter(filename);

  8.             mySerializer.Serialize(writer, config);

  9.             writer.Close();
  10.         }

  11.         public static TConfig DeserializeObject<TConfig>(string filename)
  12.             where TConfig : class
  13.         {
  14.             var mySerializer = new XmlSerializer(typeof(TConfig));

  15.             var reader = new StreamReader(filename);

  16.             var t = mySerializer.Deserialize(reader);
  17.             reader.Close();

  18.             return t as TConfig;
  19.         }
  20.     }
复制代码