码迷,mamicode.com
首页 > 其他好文 > 详细

测试用的序列化方法

时间:2014-09-10 12:13:20      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   文件   div   sp   

对于实体,进行底层方法测试的时候,经常逐一赋值很麻烦,网上找到序列化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; } }

 

测试用的序列化方法

标签:des   style   blog   color   os   io   文件   div   sp   

原文地址:http://www.cnblogs.com/starpnd/p/3964077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!