本篇內(nèi)容介紹了“如何實(shí)現(xiàn)asp.net類序列化生成xml文件”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在望都等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站制作,望都網(wǎng)站建設(shè)費(fèi)用合理。根據(jù)設(shè)計(jì)的需求需要開(kāi)發(fā)多個(gè)商品的API 原XML文件如下:
http://www.xxxxx.com/todaydetials.aspx?id=143 愛(ài)購(gòu)114 http://www.xxxxx.com/ 杭州 2011-2-9 2011-2-15 3880 2088 0.53 0
現(xiàn)在需求是要根據(jù)數(shù)據(jù)庫(kù)有幾條商品信息 相應(yīng)的API XML文件出現(xiàn)幾個(gè)URL節(jié)點(diǎn)! 采用類序列化成XML文件然后讀取相應(yīng)生成的XML文件就可以展示多個(gè)商品XML的信息 實(shí)現(xiàn)代碼如下:
首先定義好XML 各個(gè)節(jié)點(diǎn)的數(shù)據(jù)及父子節(jié)點(diǎn)的關(guān)系類:
#region 定義數(shù)據(jù)實(shí)體類xml數(shù)據(jù)結(jié)構(gòu) public class urlset { public ListurlList { get; set; } } public class url { public string loc { get; set; } public List dataList { get; set; } } public class data { public List 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 定義獲取網(wǎng)站信息實(shí)體類
public class WebSiteInfo { ////// 商品標(biāo)題 /// public string title { get; set; } ////// 商品發(fā)布時(shí)間 /// public DateTime createtime { get; set; } ////// 商品圖片 /// public string productimg { get; set; } ////// 市場(chǎng)價(jià) /// public decimal market_price { get; set; } ////// 團(tuán)購(gòu)價(jià) /// public decimal team_price { get; set; } ////// 折扣價(jià) /// public decimal zhekou_price { get; set; } ////// 城市名稱 /// public string cityName { get; set; } ////// 商品開(kāi)始時(shí)間 /// public DateTime begin_time { get; set; } ////// 結(jié)束時(shí)間 /// public DateTime end_time { get; set; } ////// 商家名稱 /// public string merchants_id { get; set; } ////// 本單詳情 /// public string description { get; set; } ////// 最低購(gòu)買人數(shù) /// public int lowBuNo { get; set; } ////// 商家地址 /// public string Address { get; set; } ////// 商家電話 /// public string Telphone { get; set; } ////// 城市區(qū)號(hào) /// public string cCode { get; set; } ////// 文件夾名稱 /// public string folderName { get; set; } ////// 團(tuán)購(gòu)狀態(tài) /// public string StatusMessage { get; set; } ////// 現(xiàn)在購(gòu)買人數(shù) /// public int nownumber { get; set; } ////// 商品編號(hào) /// public int productID { get; set; } } #endregion
第三步:獲取數(shù)據(jù)庫(kù)商品信息記錄并添加到對(duì)象的集合中(Arraylist):
#region 獲取xml實(shí)體類信息 ////// 獲取xml實(shí)體類信息 /// ///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() 0) { foreach (DataRow dr in ds.Tables[0].Rows) { WebSiteInfo webModel = new WebSiteInfo(); //城市名稱 webModel.cityName = dr["cityName"].ToString(); //商品標(biāo)題 webModel.title = dr["title"].ToString(); //商品創(chuàng)建時(shí)間 webModel.createtime = Convert.ToDateTime(dr["createtime"].ToString()); //商家名稱 webModel.merchants_id = dr["merchantsID"].ToString(); //商品圖片 webModel.productimg = dr["productimg"].ToString(); //市場(chǎng)價(jià) webModel.market_price = Convert.ToDecimal(dr["market_price"].ToString()); //團(tuán)購(gòu)價(jià) webModel.team_price = Convert.ToDecimal(dr["team_price"].ToString()); //折扣價(jià) webModel.zhekou_price = Convert.ToDecimal(dr["zhekou_price"].ToString()); //開(kāi)始時(shí)間 webModel.begin_time = Convert.ToDateTime(dr["begin_time"].ToString()); //結(jié)束時(shí)間 webModel.end_time = Convert.ToDateTime(dr["end_time"].ToString()); //商品說(shuō)明 webModel.description = dr["description"].ToString(); //最低購(gòu)買數(shù)量 webModel.lowBuNo = Convert.ToInt32(dr["lowBuyNo"].ToString()); //商家電話 webModel.Telphone = dr["Tel"].ToString(); //商家地址 webModel.Address = dr["Address"].ToString(); //城市編號(hào) webModel.cCode = dr["cCode"].ToString(); //圖片文件夾名稱 webModel.folderName = dr["prodCode"].ToString(); //現(xiàn)在購(gòu)買人數(shù) webModel.nownumber = Convert.ToInt32(dr["now_number"].ToString()); //商品編號(hào) webModel.productID = Convert.ToInt32(dr["id"].ToString()); int status = Convert.ToInt32(dr["statue"].ToString()); switch (status) { case 0: webModel.StatusMessage = "結(jié)束"; break; case 1: webModel.StatusMessage = "成功"; break; } list.Add(webModel); } } return list; } #endregion
最后一步將數(shù)據(jù)庫(kù)讀取來(lái)的信息賦值到XML 數(shù)據(jù)類型中 并序列化成XML文件保存成XML格式的文件讀取文件展現(xiàn)到界面:
#region 頁(yè)面加載 根據(jù)數(shù)據(jù)庫(kù)商品記錄數(shù)生成xml文件信息 ////// 頁(yè)面加載 根據(jù)數(shù)據(jù)庫(kù)商品記錄數(shù)生成xml文件信息 /// ListurlList = null; urlset urlsetList = new urlset(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ArrayList listinfo=GetWebModelInfo(); urlList = new List (); for (int i = 0; i < listinfo.Count; i++) { WebSiteInfo webInfo = listinfo[i] as WebSiteInfo; List displayList = new List (); display display = new display(); display.website = "愛(ài)購(gòu)114"; display.siteurl = "/tupian/20230522/xxxxx.com //城市名稱 display.city = webInfo.cityName; //商品標(biāo)題 display.webSitetitle = webInfo.title; //商品圖片 display.image = "http://211.155.235.30/tuangou/" + webInfo.folderName + "/" + webInfo.productimg; //商品開(kāi)始時(shí)間 display.startTime = webInfo.begin_time.ToShortDateString(); //商品結(jié)束時(shí)間 display.endTime = webInfo.end_time.ToShortDateString(); //市場(chǎng)價(jià) display.value = Convert.ToDouble(webInfo.market_price); //團(tuán)購(gòu)價(jià) display.price = Convert.ToDouble(webInfo.team_price); //折扣價(jià) display.rebate = Convert.ToDouble(webInfo.zhekou_price); //現(xiàn)在購(gòu)買的人數(shù) display.bought = webInfo.nownumber; displayList.Add(display); List dataList = new List(); data data = new data(); data.displayList = displayList; dataList.Add(data); url url = new url(); url.loc = String.Format("/tupian/20230522/xxxxx.com url.dataList = dataList; urlList.Add(url); urlsetList.urlList = urlList; } try { XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); xmlns.Add(String.Empty, String.Empty); //構(gòu)造字符串 StringBuilder sb = new StringBuilder(); //將字符串寫入到stringWriter對(duì)象中 StringWriter sw = new StringWriter(sb); //xml序列化對(duì)象 typeof(類名) XmlSerializer ser = new XmlSerializer(typeof(urlset)); //把Stream對(duì)象和urlset一起傳入,序列化出一個(gè)字符串sb ser.Serialize(sw, urlsetList, xmlns); sw.Close(); string FILE_NAME = HttpContext.Current.Server.MapPath("API/54tuan.xml"); FileInfo fi = new FileInfo(FILE_NAME); //如果文件己經(jīng)存在則刪除該文件 if (fi.Exists) { if (fi.Attributes.ToString().IndexOf("ReadOnly") >= 0) { fi.Attributes = FileAttributes.Normal; } File.Delete(fi.Name); } //創(chuàng)建文件 并寫入字符串 using (StreamWriter sWrite = File.CreateText(FILE_NAME)) { sWrite.Write(sb.ToString().Replace("encoding=/"utf-16/"", "encoding=/"utf-8/"").Replace(" ", "").Replace(" ", "").Replace("", "").Replace("", "").Replace(" ", "").Replace(" ", "")); 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
“如何實(shí)現(xiàn)asp.net類序列化生成xml文件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!