Detailed description of xml file instance generated by asp.net class serialization, serialization of xml

Source: Internet
Author: User

Detailed description of xml file instance generated by asp.net class serialization, serialization of xml

This example describes how to generate an xml file by serializing the asp.net class. We will share this with you for your reference. The details are as follows:

The original XML file of the API for Developing Multiple Products is as follows:

<Urlset> <url> <loc> http://www.xxxxx.com/todaydetials.aspx? Id = 143 </loc> <data> <display> <website> love buy 114 </website> <siteurl> http://www.xxxxx.com/</siteurl> <city> Hangzhou </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>

The requirement is to display several URL nodes in the corresponding api xml file based on the database's product information! The following code serializes a class into an XML file and then reads the generated XML file to display the XML Information of multiple products:

First, define the data of each XML node and the relationship class of the Parent and Child Nodes:

# Region defines the xml data structure of the data entity class 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

Step 2: # region defines the entity class for obtaining website information

Public class WebSiteInfo {// <summary> // item title // </summary> public string title {get; set ;} /// <summary> /// product release time /// </summary> public DateTime createtime {get; set ;} /// <summary> /// product image /// </summary> public string productimg {get; set ;} /// <summary> /// market price /// </summary> public decimal market_price {get; set ;} /// <summary> /// group price /// </summary> public decimal team_price {get; set ;} /// <summary> /// discount price /// </summary> public decimal zhekou_price {get; set ;} /// <summary> // city name // </summary> public string cityName {get; set ;} /// <summary> /// product start time /// </summary> public DateTime begin_time {get; set ;} /// <summary> /// End Time /// </summary> public DateTime end_time {get; set ;} /// <summary> /// merchant name /// </summary> public string merchants_id {get; set ;} /// <summary> /// details of this ticket /// </summary> public string description {get; set ;} /// <summary> /// minimum number of buyers /// </summary> public int lowBuNo {get; set ;} /// <summary> /// merchant Address // </summary> public string Address {get; set ;} /// <summary> /// merchant's phone number /// </summary> public string Telphone {get; set ;} /// <summary> // city code // </summary> public string cCode {get; set ;} /// <summary> /// folder name // </summary> public string folderName {get; set ;} /// <summary> /// group buying status /// </summary> public string StatusMessage {get; set ;} /// <summary> /// current number of buyers /// </summary> public int nownumber {get; set ;} /// <summary> /// product no. // </summary> public int productID {get; set ;}# endregion

Step 3: Obtain the database product information record and add it to the Arraylist of the object ):

# Region obtain xml Entity class information /// <summary> // obtain xml Entity class information /// </summary> /// <returns> </returns> public static arrayList GetWebModelInfo () {ArrayList list = new ArrayList (); string strSQL = "select. id,. merchantsID,. cCode,. prodCode,. statue,. now_number,. title,. createtime,. productimg,. market_price,. team_price,. zhekou_price,. cityName,. begin_time,. end_time,. description,. lowBuyNo, B. address, B. tel from tg_product as a left join tg_merchants as B on. merchantsID = B. merchants_id where. ispublic = 1 and statue>-1 and getdate () <dateadd (day, 1,. end_time) order by. 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 (); // The city name webModel. cityName = dr ["cityName"]. toString (); // The product title webModel. title = dr ["title"]. toString (); // product creation time webModel. createtime = Convert. toDateTime (dr ["createtime"]. toString (); // The Merchant name webModel. merchants_id = dr ["merchantsID"]. toString (); // product image webModel. productimg = dr ["productimg"]. toString (); // market price webModel. market_price = Convert. toDecimal (dr ["market_price"]. toString (); // The group price webModel. team_price = Convert. toDecimal (dr ["team_price"]. toString (); // discount price webModel. zhekou_price = Convert. toDecimal (dr ["zhekou_price"]. toString (); // start time webModel. begin_time = Convert. toDateTime (dr ["begin_time"]. toString (); // End Time webModel. end_time = Convert. toDateTime (dr ["end_time"]. toString (); // product description webModel. description = dr ["description"]. toString (); // the minimum number of purchased webmodels. lowBuNo = Convert. toInt32 (dr ["lowBuyNo"]. toString (); // merchant's webModel. telphone = dr ["Tel"]. toString (); // The Merchant address webModel. address = dr ["Address"]. toString (); // city number webModel. cCode = dr ["cCode"]. toString (); // The Name Of The image folder webModel. folderName = dr ["prodCode"]. toString (); // number of people who are currently buying webModel. nownumber = Convert. toInt32 (dr ["now_number"]. toString (); // product no. webModel. productID = Convert. toInt32 (dr ["id"]. toString (); int status = Convert. toInt32 (dr ["statue"]. toString (); switch (status) {case 0: webModel. statusMessage = "end"; break; case 1: webModel. statusMessage = "successful"; break;} list. add (webModel) ;}} return list ;}# endregion

The last step is to assign the Information read from the database to the XML data type and serialize it into an XML file and save it as a file in XML format to display the file to the interface:

# Region page loading: generate xml file information based on the number of database product records /// <summary> /// page loading: generate xml file information based on the number of database product records /// </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 = new display (); display. website = "purchase 114"; display. siteurl =" http://www.xxxxx.com/ "; // City name display. city = webInfo. cityName; // product title display. webSitetitle = webInfo. title; // product image display. image =" http://211.155.235.30/tuangou/ "+ WebInfo. folderName + "/" + webInfo. productimg; // display the product start time. startTime = webInfo. begin_time.tow.datestring (); // display the end time of the product. endTime = webInfo. end_time.to1_datestring (); // market price display. value = Convert. toDouble (webInfo. market_price); // The group price display. price = Convert. toDouble (webInfo. team_price); // display the discount price. rebate = Convert. toDouble (webInfo. zhekou_price); // display the current number of users. bought = webInfo. nownumber; displayList. add (display); List <data> dataList = new List <data> (); data = new data (); data. displayList = displayList; dataList. add (data); 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); // construct the string StringBuilder sb = new StringBuilder (); // write the string to the stringWriter object StringWriter sw = new StringWriter (sb ); // xml serialization object typeof (Class Name) XmlSerializer ser = new XmlSerializer (typeof (urlset); // transmits Stream object and urlset together to serialize a string 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 the file already exists, delete the file if (fi. exists) {if (fi. attributes. toString (). indexOf ("ReadOnly")> = 0) {fi. attributes = FileAttributes. normal;} File. delete (fi. name);} // create a File and write the string 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 ();} // output the xml file Response after serialization. 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

I hope this article will help you design your asp.net program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.