The example in this article describes how the ASP.net class serializes the generation of XML files. Share to everyone for your reference, specific as follows:
According to the requirements of the design needs to develop multiple products API original XML file is as follows:
<urlset>
<url>
<loc>http://www.xxxxx.com/todaydetials.aspx?id=143</loc>
<data>
<display>
<website> love to 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>
Now the requirement is to have several URL nodes based on the database having several commodity information corresponding to the API XML file! By using class serialization into an XML file and then reading the corresponding generated XML file, you can display the information implementation code for multiple commodity XML as follows:
First, define the data for each node in the XML and the relationship classes of the parent-child node:
#region defines the data entity class XML data structure 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
{
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 two: #region definition get site Information entity class
public class Websiteinfo {///<summary>///commodity header///</summary> public string Title {get; set;}
<summary>///Product release time///</summary> public DateTime createtime {get; set;}
<summary>///product picture///</summary> public string productimg {get; set;}
<summary>///Market price///</summary> public decimal Market_price {get; set;}
<summary>///Group Purchase///</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>///This detail///</summary> public string Description {get; set;}
<summary>///Minimum number of buyers///</summary> public int Lowbuno {get; set;}
<summary>///Merchant addresses///</summary> public string Addressing {get; set;}
<summary>///Merchant Phone///</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 Buy status///</summary> public string StatusMessage {get; set;}
<summary>///now purchase number///</summary> public int Nownumber {get; set;}
<summary>///Item number///</summary> public int ProductID {get; set;}}
#endregion
Step three: Get the database merchandise information record and add it to the collection of Objects (Arraylist):
#region Get XML entity class information///<summary>///get XML entity class information///</summary>///<returns></returns> public STA
Tic 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.ISPUBL
Ic=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 (); City name webmodel.cityname = dr["CityName"].
ToString (); Product title Webmodel.title = dr["title"].
ToString (); Product creation Time Webmodel.createtimE = Convert.todatetime (dr["Createtime").
ToString ()); Merchant Name webmodel.merchants_id = dr["Merchantsid"].
ToString (); Product Picture webmodel.productimg = dr["Productimg"].
ToString (); Market price Webmodel.market_price = Convert.todecimal (dr["Market_price").
ToString ()); Group Purchase 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 (); Minimum Purchase Quantity Webmodel.lowbuno = Convert.ToInt32 (dr["Lowbuyno").
ToString ()); Merchant Telephone Webmodel.telphone = dr["Tel"].
ToString (); Merchant Address webmodel.address = dr["addresses".
ToString (); City number Webmodel.ccode = dr["Ccode"].
ToString (); Picture folder name Webmodel.foldername = dr["Prodcode"]. Tostring(); The number of buyers now Webmodel.nownumber = Convert.ToInt32 (dr["Now_number").
ToString ()); Item number 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 = "Success";
Break } list.
ADD (Webmodel);
} return list;
} #endregion
The last step is to assign the information read from the database to an XML data type and serialize the file to an XML file to be saved in XML format. Read the file to the interface:
#region page load generates XML file information based on database commodity records///<summary>///page load generates XML file information based on database commodity records///</summary> list<url> ur
Llist = 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 = "Love buys 114";
Display.siteurl = "http://www.xxxxx.com/";
City name display.city = Webinfo.cityname;
Commodity title Display.websitetitle = Webinfo.title;
Product picture Display.image = "http://211.155.235.30/tuangou/" + webinfo.foldername + "/" + webinfo.productimg; Product start Time Display.starttime = Webinfo.begin_time.
ToShortDateString (); Product End Time Display.endtime = Webinfo.end_time.
ToShortDateString (); Market Price dispLay.value = convert.todouble (Webinfo.market_price);
Group Purchase Display.price = convert.todouble (Webinfo.team_price);
Discounted price display.rebate = convert.todouble (Webinfo.zhekou_price);
The number of people buying now 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);
Construct string StringBuilder sb = new StringBuilder ();
Writes a string to the StringWriter object StringWriter sw = new StringWriter (SB);
XML Serialization Object typeof (class name) XmlSerializer Ser = new XmlSerializer (typeof (Urlset)); The stream object and the UrlsetPassed together, serializing a string of 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 file and write 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 serialized XML file Response.clearcontent ();
Response.ClearHeaders ();
Response.ContentType = "Application/xml"; Response.WriteFile (HttpContext.Current.Server.MapPath ("Api/54tuan.xml"));
Response.Flush ();
Response.close (); The catch (Exception ex) {Response.Write (ex).
message);
} finally {}}} #endregion
I hope this article will help you with ASP.net programming.