Asp.net源码专业站
首页->企业网站->中小企业网站系统前台源码(SmallBusinessStarterKit)>>App-Code/Catalog/XmlCatalogProvider.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:中小企业网站系统前台源码(SmallBusinessStarterKit)
当前文件:文件类型 SmallBusinessStarterKit/App_Code/Catalog/XmlCatalogProvider.cs[6K,2009-6-12 11:54:21]打开代码结构图
普通视图
		            
1using System; 2using System.Data; 3using System.Configuration; 4using System.Web; 5using System.Xml; 6using System.Xml.Schema; 7using System.IO; 8using System.Collections.Generic; 9/// <summary> 10///产品和分类的XML数据存 11/// </summary> 12public class XmlCatalogProvider : CatalogProvider 13{ 14 private string _xmlFile; 15 private string _xsdFile; 16 17 //构造函数,从Web.Config配置文件中获取Xml文件和Schema文件。 18 public XmlCatalogProvider() 19 { 20 SmallBusinessDataProvidersSection sec = (ConfigurationManager.GetSection("SmallBusinessDataProviders")) as SmallBusinessDataProvidersSection; 21 string xmlFile = sec.CatalogProviders[sec.CatalogProviderName].Parameters["dataFile"]; 22 string xsdFile = sec.CatalogProviders[sec.CatalogProviderName].Parameters["schemaFile"]; 23 //将文件路径映射为网站虚拟路径。 24 _xmlFile = HttpContext.Current.Request.MapPath("~/App_Data/" + xmlFile); 25 _xsdFile = HttpContext.Current.Request.MapPath("~/App_Data/schemas/" + xsdFile); 26 27 } 28 /// <summary> 29 /// 返回指定分类ID的子分类ID列表 30 /// 如果分类ID为空,则返回顶层分类的列表 31 /// </summary> 32 public override List<Category> GetChildCategories(string parentCategoryId) 33 { 34 List<Category> list = new List<Category>(); 35 if (String.IsNullOrEmpty(parentCategoryId)) parentCategoryId = "NULL"; 36 //调用Util静态公共类的ReadAndValiDateXml方法从Xml和Schema文件中读取 37 //XML数据并返回一个DataSet对象。 38 DataSet dataSet = Util.ReadAndValidateXml(_xmlFile,_xsdFile); 39 //表按照在DataSet中的顺序,Category|childItemId|item. 40 DataTable categoryTbl = dataSet.Tables[0]; 41 //遍历Category表中的行信息。 42 foreach (DataRow r in categoryTbl.Rows) 43 { 44 if ((string)r["parentCategoryId"] == parentCategoryId) // match found 45 { 46 if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull) 47 throw new InvalidOperationException(Messages.CategoryRequiredAttributesMissing); 48 //使用DataRow中的数据赋值给Category对象。 49 Category curr = new Category((string)r["id"], Boolean.Parse((string)r["visible"]), (string)r["title"]); 50 curr.Description = (r["description"] is DBNull)? String.Empty : (string)r["description"]; 51 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 52 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 53 list.Add(curr); 54 } 55 } 56 //返回List<Category>对象 57 return list; 58 } 59 /// <summary> 60 /// 从一个指定的分类ID中返回产品列表 61 /// 如果分类ID为空,则返回一个空列表 62 /// </summary> 63 public override List<Item> GetChildItems(string parentCategoryId) 64 { 65 List<Item> itemList = new List<Item>(); 66 if (String.IsNullOrEmpty(parentCategoryId)) return itemList; 67 DataSet dataSet = Util.ReadAndValidateXml(_xmlFile, _xsdFile); 68 69 //根据指定的分类ID查找产品索引。 70 DataTable categoryTbl = dataSet.Tables[0]; 71 int index=-1; 72 int counter =0; 73 foreach (DataRow r in categoryTbl.Rows) 74 { 75 if ((string)r["id"] == parentCategoryId) 76 { 77 index = counter; 78 break; 79 } 80 counter++; 81 } 82 //保存子产品ID号列表然后构造产品对象。 83 DataTable categorizationTbl = dataSet.Tables[1]; 84 List<String> childItemIds = new List<String>(); 85 foreach (DataRow r in categorizationTbl.Rows) 86 { 87 if ((int)r["category_Id"] == index) 88 { 89 childItemIds.Add((string)r["childItemId_Text"]); 90 } 91 } 92 //遍历item表。 93 DataTable itemsTbl = dataSet.Tables[2]; 94 Item curr; 95 foreach (DataRow r in itemsTbl.Rows) 96 { 97 if (childItemIds.Contains((string)r["id"])) 98 { 99 if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull) 100 throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing); 101 curr = new Item((string)r["id"], 102 Boolean.Parse((string)r["visible"]), 103 (string)r["title"]); 104 curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"]; 105 curr.Price = (r["inStock"] is DBNull) ? Double.MinValue : Double.Parse((string)r["price"]); 106 curr.InStock = (r["inStock"] is DBNull) ? true : Boolean.Parse((string)r["inStock"]); 107 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 108 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 109 itemList.Add(curr); 110 } 111 } 112 return itemList; 113 } 114 ///<summary> 115 /// 返回指定产品ID号的产品 116 ///</summary> 117 public override Item GetItem(string itemId) 118 { 119 if (String.IsNullOrEmpty(itemId)) return null; 120 DataSet dataSet = Util.ReadAndValidateXml(_xmlFile, _xsdFile); 121 122 DataTable itemsTbl = dataSet.Tables[2]; 123 Item curr=null; 124 foreach (DataRow r in itemsTbl.Rows) 125 { 126 if (r["id"] is DBNull ) 127 throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing); 128 129 if(itemId == (string)r["id"]) 130 { 131 // ID号不能为空。 132 if (r["visible"] is DBNull || r["title"] is DBNull) 133 throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing); 134 135 curr = new Item((string)r["id"],Boolean.Parse((string)r["visible"]),(string)r["title"]); 136 curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"]; 137 curr.Price = (r["price"] is DBNull) ? Double.MinValue : Double.Parse((string)r["price"]); 138 curr.InStock = (r["inStock"] is DBNull) ? true : Boolean.Parse((string)r["inStock"]); 139 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 140 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 141 } 142 } 143 return curr; 144 } 145} 146
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:中小企业网站系统前台源码(SmallBusinessStarterKit)
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146