对于实体,进行底层方法测试的时候,经常逐一赋值很麻烦,网上找到序列化xml方法,感觉挺好用的。
前端调用方法时,将实体序列化写入xml文件
//xml路径 string filePath = @"D:\1.xml"; using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) { //fileinfo为实体名称 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(fileinfo.GetType()); xs.Serialize(writer, fileinfo); writer.Close(); }
测试底层方法时,通过读取xml获得实体
//xml路径 string filePath = @"D:\1.xml"; if (System.IO.File.Exists(filePath)) { using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) { //fileinfo为待测试的实体名称 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(fileinfo.GetType()); object obj = xs.Deserialize(reader); reader.Close(); //FileInfo_Logistic_Entity为待测试的实体类型 fileinfo = obj as FileInfo_Logistic_Entity; } }
时间: 2024-11-03 23:47:39