千家信息网

如何实现asp.net类序列化生成xml文件

发表于:2024-10-06 作者:千家信息网编辑
千家信息网最后更新 2024年10月06日,本篇内容介绍了"如何实现asp.net类序列化生成xml文件"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有
千家信息网最后更新 2024年10月06日如何实现asp.net类序列化生成xml文件

本篇内容介绍了"如何实现asp.net类序列化生成xml文件"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

   http://www.xxxxx.com/todaydetials.aspx?id=143         爱购114    http://www.xxxxx.com/    杭州            2011-2-9    2011-2-15    3880    2088    0.53    0       

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

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

#region 定义数据实体类xml数据结构public class urlset{  public List urlList  {   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 定义获取网站信息实体类

public class WebSiteInfo{  ///   /// 商品标题  ///   public string title { get; set; }  ///   /// 商品发布时间  ///   public DateTime createtime { get; set; }  ///   /// 商品图片  ///   public string productimg { get; set; }  ///   /// 市场价  ///   public decimal market_price { get; set; }  ///   /// 团购价  ///   public decimal team_price { get; set; }  ///   /// 折扣价  ///   public decimal zhekou_price { get; set; }  ///   /// 城市名称   ///   public string cityName { get; set; }  ///   /// 商品开始时间  ///   public DateTime begin_time { get; set; }  ///   /// 结束时间  ///   public DateTime end_time { get; set; }  ///   /// 商家名称  ///   public string merchants_id { get; set; }  ///   /// 本单详情  ///   public string description { get; set; }  ///   /// 最低购买人数  ///   public int lowBuNo { get; set; }  ///   /// 商家地址  ///   public string Address { get; set; }  ///   /// 商家电话  ///   public string Telphone { get; set; }  ///   /// 城市区号  ///   public string cCode { get; set; }  ///   /// 文件夹名称  ///   public string folderName { get; set; }  ///   /// 团购状态   ///   public string StatusMessage { get; set; }  ///   /// 现在购买人数  ///   public int nownumber { get; set; }  ///   /// 商品编号  ///   public int productID { get; set; }}#endregion

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

#region 获取xml实体类信息/// /// 获取xml实体类信息/// /// 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();    //商品标题    webModel.title = dr["title"].ToString();    //商品创建时间    webModel.createtime = Convert.ToDateTime(dr["createtime"].ToString());    //商家名称    webModel.merchants_id = dr["merchantsID"].ToString();    //商品图片    webModel.productimg = dr["productimg"].ToString();    //市场价    webModel.market_price = Convert.ToDecimal(dr["market_price"].ToString());    //团购价    webModel.team_price = Convert.ToDecimal(dr["team_price"].ToString());    //折扣价    webModel.zhekou_price = Convert.ToDecimal(dr["zhekou_price"].ToString());    //开始时间    webModel.begin_time = Convert.ToDateTime(dr["begin_time"].ToString());    //结束时间    webModel.end_time = Convert.ToDateTime(dr["end_time"].ToString());    //商品说明    webModel.description = dr["description"].ToString();    //最低购买数量    webModel.lowBuNo = Convert.ToInt32(dr["lowBuyNo"].ToString());    //商家电话    webModel.Telphone = dr["Tel"].ToString();    //商家地址    webModel.Address = dr["Address"].ToString();    //城市编号    webModel.cCode = dr["cCode"].ToString();    //图片文件夹名称    webModel.folderName = dr["prodCode"].ToString();    //现在购买人数    webModel.nownumber = Convert.ToInt32(dr["now_number"].ToString());    //商品编号    webModel.productID = Convert.ToInt32(dr["id"].ToString());    int status = Convert.ToInt32(dr["statue"].ToString());    switch (status)    {     case 0:      webModel.StatusMessage = "结束";      break;     case 1:      webModel.StatusMessage = "成功";      break;    }    list.Add(webModel);   }  }   return list;}#endregion

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

#region 页面加载 根据数据库商品记录数生成xml文件信息/// /// 页面加载 根据数据库商品记录数生成xml文件信息/// List urlList = 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 = "爱购114";    display.siteurl = "http://www.xxxxx.com/";    //城市名称    display.city = webInfo.cityName;    //商品标题    display.webSitetitle = webInfo.title;    //商品图片    display.image = "http://211.155.235.30/tuangou/" + webInfo.folderName + "/" + webInfo.productimg;    //商品开始时间    display.startTime = webInfo.begin_time.ToShortDateString();    //商品结束时间    display.endTime = webInfo.end_time.ToShortDateString();    //市场价    display.value = Convert.ToDouble(webInfo.market_price);    //团购价    display.price = Convert.ToDouble(webInfo.team_price);    //折扣价    display.rebate = Convert.ToDouble(webInfo.zhekou_price);    //现在购买的人数    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("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);    //构造字符串    StringBuilder sb = new StringBuilder();    //将字符串写入到stringWriter对象中    StringWriter sw = new StringWriter(sb);    //xml序列化对象 typeof(类名)    XmlSerializer ser = new XmlSerializer(typeof(urlset));    //把Stream对象和urlset一起传入,序列化出一个字符串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 (fi.Exists)    {     if (fi.Attributes.ToString().IndexOf("ReadOnly") >= 0) {      fi.Attributes = FileAttributes.Normal;     }     File.Delete(fi.Name);    }    //创建文件 并写入字符串    using (StreamWriter sWrite = File.CreateText(FILE_NAME))    {     sWrite.Write(sb.ToString().Replace("encoding=/"utf-16/"", "encoding=/"utf-8/"").Replace("", "").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

"如何实现asp.net类序列化生成xml文件"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0