public string GetXml(productList list) { StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); sb.Append("<productList>\r\n"); sb.Append("<timestamp>" + Utils.ToString(list.timestamp) + "</timestamp>\r\n"); sb.Append("<status>" + Utils.ToString(list.status) + "</status>\r\n"); sb.Append("<message>" + Utils.ToString(list.message) + "</message>\r\n"); if (list.products != null && list.products.Count > 0) { foreach (var p in list.products) { sb.Append("<product>\r\n"); var properties = TypeDescriptor.GetProperties(p); foreach (PropertyDescriptor propertyDescriptror in properties) { sb.Append("\t<" + propertyDescriptror.Name + ">" + Utils.ToString(propertyDescriptror.GetValue(p)) + "</" + propertyDescriptror.Name + ">\r\n"); } sb.Append("</product>\r\n"); } } sb.Append("</productList>"); return sb.ToString(); }
/// <summary> /// 获取产品信息错误 /// </summary> /// <returns></returns> public void FormatXML(string status, string message, List<Product_Cache> list, List<Product_Category_Brand> list2) { productList result = new productList(); result.status = status; result.message = message; result.timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (list != null && list.Count > 0 && list2 != null && list2.Count > 0) { result.products = (from p in list select new product { id = p.ProductID, name = "<![CDATA[" + p.ProductName + "]]>", barcode = p.Barcode, spec = "", price = p.PromotePrice.ToString("#0.00"), sale = string.IsNullOrEmpty(p.PromoteIDs) ? 0 : 1, stock = p.InventoryNumber > 0 ? 1 : 0, category = GetCategory(p.ProductCategoryID, list2), brand = GetBrand(p.ProductBrandID, list2), wapUrl = "", webUrl = "http://www.gjw.com/product/item-id-" + Utils.ToString(p.ProductID) + ".htm", updatetime = System.DateTime.Now }).ToList(); } Response.Clear(); Response.ContentType = "text/xml"; Response.Charset = "UTF-8"; Response.Write(GetXml(result)); Response.Flush(); Response.End(); }