测试用的序列化方法

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

 

posted @ 2014-09-10 11:30  了了之  阅读(249)  评论(0编辑  收藏  举报