本文實例講述了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
新聞熱點
疑難解答
圖片精選