asp.net类序列化生成xml文件实例详解
程序员文章站
2024-02-12 09:31:25
本文实例讲述了asp.net类序列化生成xml文件的方法。分享给大家供大家参考,具体如下:
根据设计的需求需要开发多个商品的api 原xml文件如下:
<...
本文实例讲述了asp.net类序列化生成xml文件的方法。分享给大家供大家参考,具体如下:
根据设计的需求需要开发多个商品的api 原xml文件如下:
<urlset> <url> <loc>http://www.xxxxx.com/todaydetials.aspx?id=143</loc> <data> <display> <website>爱购114</website> <siteurl>http://www.xxxxx.com/</siteurl> <city>杭州</city> <websitetitle></websitetitle> <image></image> <starttime>2011-2-9</starttime> <endtime>2011-2-15</endtime> <value>3880</value> <price>2088</price> <rebate>0.53</rebate> <bought>0</bought> </display> </data> </url> </urlset>
现在需求是要根据数据库有几条商品信息 相应的api xml文件出现几个url节点! 采用类序列化成xml文件然后读取相应生成的xml文件就可以展示多个商品xml的信息 实现代码如下:
首先定义好xml 各个节点的数据及父子节点的关系类:
#region 定义数据实体类xml数据结构 public class urlset { public list<url> urllist { get; set; } } public class url { public string loc { get; set; } public list<data> datalist { get; set; } } public class data { public list<display> displaylist { get; set; } } public class display { public string website { get; set; } public string siteurl { get; set; } public string city { get; set; } public string websitetitle { get; set; } public string image { get; set; } public string starttime { get; set; } public string endtime { get; set; } public double value { get; set; } public double price { get; set; } public double rebate { get; set; } public int bought { get; set; } } #endregion
第二步:#region 定义获取网站信息实体类
public class websiteinfo { /// <summary> /// 商品标题 /// </summary> public string title { get; set; } /// <summary> /// 商品发布时间 /// </summary> public datetime createtime { get; set; } /// <summary> /// 商品图片 /// </summary> public string productimg { get; set; } /// <summary> /// 市场价 /// </summary> public decimal market_price { get; set; } /// <summary> /// 团购价 /// </summary> public decimal team_price { get; set; } /// <summary> /// 折扣价 /// </summary> public decimal zhekou_price { get; set; } /// <summary> /// 城市名称 /// </summary> public string cityname { get; set; } /// <summary> /// 商品开始时间 /// </summary> public datetime begin_time { get; set; } /// <summary> /// 结束时间 /// </summary> public datetime end_time { get; set; } /// <summary> /// 商家名称 /// </summary> public string merchants_id { get; set; } /// <summary> /// 本单详情 /// </summary> public string description { get; set; } /// <summary> /// 最低购买人数 /// </summary> public int lowbuno { get; set; } /// <summary> /// 商家地址 /// </summary> public string address { get; set; } /// <summary> /// 商家电话 /// </summary> public string telphone { get; set; } /// <summary> /// 城市区号 /// </summary> public string ccode { get; set; } /// <summary> /// 文件夹名称 /// </summary> public string foldername { get; set; } /// <summary> /// 团购状态 /// </summary> public string statusmessage { get; set; } /// <summary> /// 现在购买人数 /// </summary> public int nownumber { get; set; } /// <summary> /// 商品编号 /// </summary> public int productid { get; set; } } #endregion
第三步:获取数据库商品信息记录并添加到对象的集合中(arraylist):
#region 获取xml实体类信息 /// <summary> /// 获取xml实体类信息 /// </summary> /// <returns></returns> public static arraylist getwebmodelinfo() { arraylist list = new arraylist(); string strsql = "select a.id, a.merchantsid,a.ccode,a.prodcode,a.statue,a.now_number, a.title,a.createtime,a.productimg,a.market_price,a.team_price,a.zhekou_price,a.cityname,a.begin_time,a.end_time,a.description,a.lowbuyno,b.address,b.tel from tg_product as a left join tg_merchants as b on a.merchantsid=b.merchants_id where a.ispublic=1 and statue>-1 and getdate()<dateadd(day,1,a.end_time) order by a.createtime desc"; dataset ds = framework.data.sqlhelper.returndataset(commandtype.text, strsql, null); if (ds.tables[0].rows.count > 0) { foreach (datarow dr in ds.tables[0].rows) { websiteinfo webmodel = new websiteinfo(); //城市名称 webmodel.cityname = dr["cityname"].tostring(); //商品标题 webmodel.title = dr["title"].tostring(); //商品创建时间 webmodel.createtime = convert.todatetime(dr["createtime"].tostring()); //商家名称 webmodel.merchants_id = dr["merchantsid"].tostring(); //商品图片 webmodel.productimg = dr["productimg"].tostring(); //市场价 webmodel.market_price = convert.todecimal(dr["market_price"].tostring()); //团购价 webmodel.team_price = convert.todecimal(dr["team_price"].tostring()); //折扣价 webmodel.zhekou_price = convert.todecimal(dr["zhekou_price"].tostring()); //开始时间 webmodel.begin_time = convert.todatetime(dr["begin_time"].tostring()); //结束时间 webmodel.end_time = convert.todatetime(dr["end_time"].tostring()); //商品说明 webmodel.description = dr["description"].tostring(); //最低购买数量 webmodel.lowbuno = convert.toint32(dr["lowbuyno"].tostring()); //商家电话 webmodel.telphone = dr["tel"].tostring(); //商家地址 webmodel.address = dr["address"].tostring(); //城市编号 webmodel.ccode = dr["ccode"].tostring(); //图片文件夹名称 webmodel.foldername = dr["prodcode"].tostring(); //现在购买人数 webmodel.nownumber = convert.toint32(dr["now_number"].tostring()); //商品编号 webmodel.productid = convert.toint32(dr["id"].tostring()); int status = convert.toint32(dr["statue"].tostring()); switch (status) { case 0: webmodel.statusmessage = "结束"; break; case 1: webmodel.statusmessage = "成功"; break; } list.add(webmodel); } } return list; } #endregion
最后一步将数据库读取来的信息赋值到xml 数据类型中 并序列化成xml文件保存成xml格式的文件读取文件展现到界面:
#region 页面加载 根据数据库商品记录数生成xml文件信息 /// <summary> /// 页面加载 根据数据库商品记录数生成xml文件信息 /// </summary> list<url> urllist = null; urlset urlsetlist = new urlset(); protected void page_load(object sender, eventargs e) { if (!page.ispostback) { arraylist listinfo=getwebmodelinfo(); urllist = new list<url>(); for (int i = 0; i < listinfo.count; i++) { websiteinfo webinfo = listinfo[i] as websiteinfo; list<display> displaylist = new list<display>(); display display = new display(); display.website = "爱购114"; display.siteurl = "http://www.xxxxx.com/"; //城市名称 display.city = webinfo.cityname; //商品标题 display.websitetitle = webinfo.title; //商品图片 display.image = "http://211.155.235.30/tuangou/" + webinfo.foldername + "/" + webinfo.productimg; //商品开始时间 display.starttime = webinfo.begin_time.toshortdatestring(); //商品结束时间 display.endtime = webinfo.end_time.toshortdatestring(); //市场价 display.value = convert.todouble(webinfo.market_price); //团购价 display.price = convert.todouble(webinfo.team_price); //折扣价 display.rebate = convert.todouble(webinfo.zhekou_price); //现在购买的人数 display.bought = webinfo.nownumber; displaylist.add(display); list<data> datalist = new list<data>(); data data = new data(); data.displaylist = displaylist; datalist.add(data); url url = new url(); url.loc = string.format("http://www.xxxxx.com/todaydetials.aspx?id={0}", webinfo.productid.tostring()); url.datalist = datalist; urllist.add(url); urlsetlist.urllist = urllist; } try { xmlserializernamespaces xmlns = new xmlserializernamespaces(); xmlns.add(string.empty, string.empty); //构造字符串 stringbuilder sb = new stringbuilder(); //将字符串写入到stringwriter对象中 stringwriter sw = new stringwriter(sb); //xml序列化对象 typeof(类名) xmlserializer ser = new xmlserializer(typeof(urlset)); //把stream对象和urlset一起传入,序列化出一个字符串sb ser.serialize(sw, urlsetlist, xmlns); sw.close(); string file_name = httpcontext.current.server.mappath("api/54tuan.xml"); fileinfo fi = new fileinfo(file_name); //如果文件己经存在则删除该文件 if (fi.exists) { if (fi.attributes.tostring().indexof("readonly") >= 0) { fi.attributes = fileattributes.normal; } file.delete(fi.name); } //创建文件 并写入字符串 using (streamwriter swrite = file.createtext(file_name)) { swrite.write(sb.tostring().replace("encoding=/"utf-16/"", "encoding=/"utf-8/"").replace("<urllist>", "").replace("</urllist>", "").replace("<datalist>", "").replace("</datalist>", "").replace("<displaylist>", "").replace("<displaylist>", "").replace("</displaylist>", "")); swrite.close(); } //输出序列化后xml文件 response.clearcontent(); response.clearheaders(); response.contenttype = "application/xml"; response.writefile(httpcontext.current.server.mappath("api/54tuan.xml")); response.flush(); response.close(); } catch (exception ex) { response.write(ex.message); } finally { } } } #endregion
希望本文所述对大家asp.net程序设计有所帮助。
上一篇: Java读取TXT文件内容的方法