首页 > 编程 > .NET > 正文

asp.net类序列化生成xml文件实例详解

2024-07-10 13:29:41
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了asp.net类序列化生成xml文件的方法,结合实例形式较为详细的分析了asp.net序列化生成xml文件的具体步骤与相关实现技巧,需要的朋友可以参考下

本文实例讲述了asp.net类序列化生成xml文件的方法。分享给大家供大家参考,具体如下:

根据设计的需求需要开发多个商品的API 原XML文件如下:

 

 
  1. <urlset> 
  2. <url> 
  3. <loc>http://www.xxxxx.com/todaydetials.aspx?id=143</loc> 
  4. <data> 
  5. <display> 
  6. <website>爱购114</website> 
  7. <siteurl>http://www.xxxxx.com/</siteurl> 
  8. <city>杭州</city> 
  9. <webSitetitle></webSitetitle> 
  10. <image></image> 
  11. <startTime>2011-2-9</startTime> 
  12. <endTime>2011-2-15</endTime> 
  13. <value>3880</value> 
  14. <price>2088</price> 
  15. <rebate>0.53</rebate> 
  16. <bought>0</bought> 
  17. </display>  
  18. </data> 
  19. </url> 
  20. </urlset> 

现在需求是要根据数据库有几条商品信息 相应的API XML文件出现几个URL节点! 采用类序列化成XML文件然后读取相应生成的XML文件就可以展示多个商品XML的信息 实现代码如下:

首先定义好XML 各个节点的数据及父子节点的关系类:

 

 
  1. #region 定义数据实体类xml数据结构 
  2. public class urlset 
  3. { 
  4. public List<url> urlList 
  5. { 
  6. get; 
  7. set; 
  8. } 
  9. } 
  10. public class url 
  11. { 
  12. public string loc 
  13. { 
  14. get; 
  15. set; 
  16. } 
  17. public List<data> dataList 
  18. { 
  19. get; 
  20. set; 
  21. } 
  22. } 
  23. public class data 
  24. { 
  25. public List<display> displayList 
  26. { 
  27. get; 
  28. set; 
  29. } 
  30. } 
  31. public class display 
  32. { 
  33. public string website 
  34. { 
  35. get; 
  36. set; 
  37. } 
  38. public string siteurl 
  39. { 
  40. get; 
  41. set; 
  42. } 
  43. public string city 
  44. { 
  45. get; 
  46. set; 
  47. } 
  48. public string webSitetitle 
  49. { 
  50. get; 
  51. set; 
  52. } 
  53. public string image 
  54. { 
  55. get; 
  56. set; 
  57. } 
  58. public string startTime 
  59. { 
  60. get; 
  61. set; 
  62. } 
  63. public string endTime 
  64. { 
  65. get; 
  66. set; 
  67. } 
  68. public double value 
  69. { 
  70. get; 
  71. set; 
  72. } 
  73. public double price 
  74. { 
  75. get; 
  76. set; 
  77. } 
  78. public double rebate 
  79. { 
  80. get; 
  81. set; 
  82. } 
  83. public int bought 
  84. { 
  85. get; 
  86. set; 
  87. } 
  88. } 
  89. #endregion 

第二步:#region 定义获取网站信息实体类

 

 
  1. public class WebSiteInfo 
  2. { 
  3. /// <summary> 
  4. /// 商品标题 
  5. /// </summary> 
  6. public string title { get; set; } 
  7. /// <summary> 
  8. /// 商品发布时间 
  9. /// </summary> 
  10. public DateTime createtime { get; set; } 
  11. /// <summary> 
  12. /// 商品图片 
  13. /// </summary> 
  14. public string productimg { get; set; } 
  15. /// <summary> 
  16. /// 市场价 
  17. /// </summary> 
  18. public decimal market_price { get; set; } 
  19. /// <summary> 
  20. /// 团购价 
  21. /// </summary> 
  22. public decimal team_price { get; set; } 
  23. /// <summary> 
  24. /// 折扣价 
  25. /// </summary> 
  26. public decimal zhekou_price { get; set; } 
  27. /// <summary> 
  28. /// 城市名称  
  29. /// </summary> 
  30. public string cityName { get; set; } 
  31. /// <summary> 
  32. /// 商品开始时间 
  33. /// </summary> 
  34. public DateTime begin_time { get; set; } 
  35. /// <summary> 
  36. /// 结束时间 
  37. /// </summary> 
  38. public DateTime end_time { get; set; } 
  39. /// <summary> 
  40. /// 商家名称 
  41. /// </summary> 
  42. public string merchants_id { get; set; } 
  43. /// <summary> 
  44. /// 本单详情 
  45. /// </summary> 
  46. public string description { get; set; } 
  47. /// <summary> 
  48. /// 最低购买人数 
  49. /// </summary> 
  50. public int lowBuNo { get; set; } 
  51. /// <summary> 
  52. /// 商家地址 
  53. /// </summary> 
  54. public string Address { get; set; } 
  55. /// <summary> 
  56. /// 商家电话 
  57. /// </summary> 
  58. public string Telphone { get; set; } 
  59. /// <summary> 
  60. /// 城市区号 
  61. /// </summary> 
  62. public string cCode { get; set; } 
  63. /// <summary> 
  64. /// 文件夹名称 
  65. /// </summary> 
  66. public string folderName { get; set; } 
  67. /// <summary> 
  68. /// 团购状态  
  69. /// </summary> 
  70. public string StatusMessage { get; set; } 
  71. /// <summary> 
  72. /// 现在购买人数 
  73. /// </summary> 
  74. public int nownumber { get; set; } 
  75. /// <summary> 
  76. /// 商品编号 
  77. /// </summary> 
  78. public int productID { get; set; } 
  79. } 
  80. #endregion 

第三步:获取数据库商品信息记录并添加到对象的集合中(Arraylist):

 

 
  1. #region 获取xml实体类信息 
  2. /// <summary> 
  3. /// 获取xml实体类信息 
  4. /// </summary> 
  5. /// <returns></returns> 
  6. public static ArrayList GetWebModelInfo() 
  7. { 
  8. ArrayList list = new ArrayList(); 
  9. 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"; 
  10. DataSet ds = FrameWork.Data.SqlHelper.ReturnDataSet(CommandType.Text, strSQL, null); 
  11. if (ds.Tables[0].Rows.Count > 0) 
  12. { 
  13. foreach (DataRow dr in ds.Tables[0].Rows) 
  14. { 
  15. WebSiteInfo webModel = new WebSiteInfo(); 
  16. //城市名称 
  17. webModel.cityName = dr["cityName"].ToString(); 
  18. //商品标题 
  19. webModel.title = dr["title"].ToString(); 
  20. //商品创建时间 
  21. webModel.createtime = Convert.ToDateTime(dr["createtime"].ToString()); 
  22. //商家名称 
  23. webModel.merchants_id = dr["merchantsID"].ToString(); 
  24. //商品图片 
  25. webModel.productimg = dr["productimg"].ToString(); 
  26. //市场价 
  27. webModel.market_price = Convert.ToDecimal(dr["market_price"].ToString()); 
  28. //团购价 
  29. webModel.team_price = Convert.ToDecimal(dr["team_price"].ToString()); 
  30. //折扣价 
  31. webModel.zhekou_price = Convert.ToDecimal(dr["zhekou_price"].ToString()); 
  32. //开始时间 
  33. webModel.begin_time = Convert.ToDateTime(dr["begin_time"].ToString()); 
  34. //结束时间 
  35. webModel.end_time = Convert.ToDateTime(dr["end_time"].ToString()); 
  36. //商品说明 
  37. webModel.description = dr["description"].ToString(); 
  38. //最低购买数量 
  39. webModel.lowBuNo = Convert.ToInt32(dr["lowBuyNo"].ToString()); 
  40. //商家电话 
  41. webModel.Telphone = dr["Tel"].ToString(); 
  42. //商家地址 
  43. webModel.Address = dr["Address"].ToString(); 
  44. //城市编号 
  45. webModel.cCode = dr["cCode"].ToString(); 
  46. //图片文件夹名称 
  47. webModel.folderName = dr["prodCode"].ToString(); 
  48. //现在购买人数 
  49. webModel.nownumber = Convert.ToInt32(dr["now_number"].ToString()); 
  50. //商品编号 
  51. webModel.productID = Convert.ToInt32(dr["id"].ToString()); 
  52. int status = Convert.ToInt32(dr["statue"].ToString()); 
  53. switch (status) 
  54. { 
  55. case 0: 
  56. webModel.StatusMessage = "结束"; 
  57. break; 
  58. case 1: 
  59. webModel.StatusMessage = "成功"; 
  60. break; 
  61. } 
  62. list.Add(webModel); 
  63. } 
  64. } 
  65. return list; 
  66. } 
  67. #endregion 

最后一步将数据库读取来的信息赋值到XML 数据类型中 并序列化成XML文件保存成XML格式的文件读取文件展现到界面:

 

 
  1. #region 页面加载 根据数据库商品记录数生成xml文件信息 
  2. /// <summary> 
  3. /// 页面加载 根据数据库商品记录数生成xml文件信息 
  4. /// </summary> 
  5. List<url> urlList = null; 
  6. urlset urlsetList = new urlset(); 
  7. protected void Page_Load(object sender, EventArgs e) 
  8. { 
  9. if (!Page.IsPostBack) 
  10. { 
  11. ArrayList listinfo=GetWebModelInfo(); 
  12. urlList = new List<url>(); 
  13. for (int i = 0; i < listinfo.Count; i++) 
  14. { 
  15. WebSiteInfo webInfo = listinfo[i] as WebSiteInfo; 
  16. List<display> displayList = new List<display>(); 
  17. display display = new display(); 
  18. display.website = "爱购114"; 
  19. display.siteurl = "http://www.xxxxx.com/"; 
  20. //城市名称 
  21. display.city = webInfo.cityName; 
  22. //商品标题 
  23. display.webSitetitle = webInfo.title; 
  24. //商品图片 
  25. display.image = "http://211.155.235.30/tuangou/" + webInfo.folderName + "/" + webInfo.productimg; 
  26. //商品开始时间 
  27. display.startTime = webInfo.begin_time.ToShortDateString(); 
  28. //商品结束时间 
  29. display.endTime = webInfo.end_time.ToShortDateString(); 
  30. //市场价 
  31. display.value = Convert.ToDouble(webInfo.market_price); 
  32. //团购价 
  33. display.price = Convert.ToDouble(webInfo.team_price); 
  34. //折扣价 
  35. display.rebate = Convert.ToDouble(webInfo.zhekou_price); 
  36. //现在购买的人数 
  37. display.bought = webInfo.nownumber; 
  38. displayList.Add(display); 
  39. List<data> dataList = new List<data>(); 
  40. data data = new data(); 
  41. data.displayList = displayList; 
  42. dataList.Add(data); 
  43. url url = new url(); 
  44. url.loc = String.Format("http://www.xxxxx.com/todaydetials.aspx?id={0}", webInfo.productID.ToString()); 
  45. url.dataList = dataList; 
  46. urlList.Add(url); 
  47. urlsetList.urlList = urlList; 
  48. } 
  49. try 
  50. { 
  51. XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); 
  52. xmlns.Add(String.Empty, String.Empty); 
  53. //构造字符串 
  54. StringBuilder sb = new StringBuilder(); 
  55. //将字符串写入到stringWriter对象中 
  56. StringWriter sw = new StringWriter(sb); 
  57. //xml序列化对象 typeof(类名) 
  58. XmlSerializer ser = new XmlSerializer(typeof(urlset)); 
  59. //把Stream对象和urlset一起传入,序列化出一个字符串sb 
  60. ser.Serialize(sw, urlsetList, xmlns); 
  61. sw.Close(); 
  62. string FILE_NAME = HttpContext.Current.Server.MapPath("API/54tuan.xml"); 
  63. FileInfo fi = new FileInfo(FILE_NAME); 
  64. //如果文件己经存在则删除该文件  
  65. if (fi.Exists) 
  66. { 
  67. if (fi.Attributes.ToString().IndexOf("ReadOnly") >= 0) { 
  68. fi.Attributes = FileAttributes.Normal; 
  69. } 
  70. File.Delete(fi.Name); 
  71. } 
  72. //创建文件 并写入字符串 
  73. using (StreamWriter sWrite = File.CreateText(FILE_NAME)) 
  74. { 
  75. 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>", "")); 
  76. sWrite.Close(); 
  77. } 
  78. //输出序列化后xml文件 
  79. Response.ClearContent(); 
  80. Response.ClearHeaders(); 
  81. Response.ContentType = "application/xml"; 
  82. Response.WriteFile(HttpContext.Current.Server.MapPath("API/54tuan.xml")); 
  83. Response.Flush(); 
  84. Response.Close(); 
  85. } 
  86. catch (Exception ex) 
  87. { 
  88. Response.Write(ex.Message); 
  89. } 
  90. finally 
  91. { 
  92. } 
  93. } 
  94. } 
  95. #endregion 

希望本文所述对大家asp.net程序设计有所帮助。


注:相关教程知识阅读请移步到ASP.NET教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表