?? xmlcatalogprovider.cs
字號(hào):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Collections.Generic;
/// <summary>
///產(chǎn)品和分類的XML數(shù)據(jù)存
/// </summary>
public class XmlCatalogProvider : CatalogProvider
{
private string _xmlFile;
private string _xsdFile;
//構(gòu)造函數(shù),從Web.Config配置文件中獲取Xml文件和Schema文件。
public XmlCatalogProvider()
{
SmallBusinessDataProvidersSection sec = (ConfigurationManager.GetSection("SmallBusinessDataProviders")) as SmallBusinessDataProvidersSection;
string xmlFile = sec.CatalogProviders[sec.CatalogProviderName].Parameters["dataFile"];
string xsdFile = sec.CatalogProviders[sec.CatalogProviderName].Parameters["schemaFile"];
//將文件路徑映射為網(wǎng)站虛擬路徑。
_xmlFile = HttpContext.Current.Request.MapPath("~/App_Data/" + xmlFile);
_xsdFile = HttpContext.Current.Request.MapPath("~/App_Data/schemas/" + xsdFile);
}
/// <summary>
/// 返回指定分類ID的子分類ID列表
/// 如果分類ID為空,則返回頂層分類的列表
/// </summary>
public override List<Category> GetChildCategories(string parentCategoryId)
{
List<Category> list = new List<Category>();
if (String.IsNullOrEmpty(parentCategoryId)) parentCategoryId = "NULL";
//調(diào)用Util靜態(tài)公共類的ReadAndValiDateXml方法從Xml和Schema文件中讀取
//XML數(shù)據(jù)并返回一個(gè)DataSet對(duì)象。
DataSet dataSet = Util.ReadAndValidateXml(_xmlFile,_xsdFile);
//表按照在DataSet中的順序,Category|childItemId|item.
DataTable categoryTbl = dataSet.Tables[0];
//遍歷Category表中的行信息。
foreach (DataRow r in categoryTbl.Rows)
{
if ((string)r["parentCategoryId"] == parentCategoryId) // match found
{
if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull)
throw new InvalidOperationException(Messages.CategoryRequiredAttributesMissing);
//使用DataRow中的數(shù)據(jù)賦值給Category對(duì)象。
Category curr = new Category((string)r["id"], Boolean.Parse((string)r["visible"]), (string)r["title"]);
curr.Description = (r["description"] is DBNull)? String.Empty : (string)r["description"];
curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"];
curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"];
list.Add(curr);
}
}
//返回List<Category>對(duì)象
return list;
}
/// <summary>
/// 從一個(gè)指定的分類ID中返回產(chǎn)品列表
/// 如果分類ID為空,則返回一個(gè)空列表
/// </summary>
public override List<Item> GetChildItems(string parentCategoryId)
{
List<Item> itemList = new List<Item>();
if (String.IsNullOrEmpty(parentCategoryId)) return itemList;
DataSet dataSet = Util.ReadAndValidateXml(_xmlFile, _xsdFile);
//根據(jù)指定的分類ID查找產(chǎn)品索引。
DataTable categoryTbl = dataSet.Tables[0];
int index=-1;
int counter =0;
foreach (DataRow r in categoryTbl.Rows)
{
if ((string)r["id"] == parentCategoryId)
{
index = counter;
break;
}
counter++;
}
//保存子產(chǎn)品ID號(hào)列表然后構(gòu)造產(chǎn)品對(duì)象。
DataTable categorizationTbl = dataSet.Tables[1];
List<String> childItemIds = new List<String>();
foreach (DataRow r in categorizationTbl.Rows)
{
if ((int)r["category_Id"] == index)
{
childItemIds.Add((string)r["childItemId_Text"]);
}
}
//遍歷item表。
DataTable itemsTbl = dataSet.Tables[2];
Item curr;
foreach (DataRow r in itemsTbl.Rows)
{
if (childItemIds.Contains((string)r["id"]))
{
if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull)
throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing);
curr = new Item((string)r["id"],
Boolean.Parse((string)r["visible"]),
(string)r["title"]);
curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"];
curr.Price = (r["inStock"] is DBNull) ? Double.MinValue : Double.Parse((string)r["price"]);
curr.InStock = (r["inStock"] is DBNull) ? true : Boolean.Parse((string)r["inStock"]);
curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"];
curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"];
itemList.Add(curr);
}
}
return itemList;
}
///<summary>
/// 返回指定產(chǎn)品ID號(hào)的產(chǎn)品
///</summary>
public override Item GetItem(string itemId)
{
if (String.IsNullOrEmpty(itemId)) return null;
DataSet dataSet = Util.ReadAndValidateXml(_xmlFile, _xsdFile);
DataTable itemsTbl = dataSet.Tables[2];
Item curr=null;
foreach (DataRow r in itemsTbl.Rows)
{
if (r["id"] is DBNull )
throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing);
if(itemId == (string)r["id"])
{
// ID號(hào)不能為空。
if (r["visible"] is DBNull || r["title"] is DBNull)
throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing);
curr = new Item((string)r["id"],Boolean.Parse((string)r["visible"]),(string)r["title"]);
curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"];
curr.Price = (r["price"] is DBNull) ? Double.MinValue : Double.Parse((string)r["price"]);
curr.InStock = (r["inStock"] is DBNull) ? true : Boolean.Parse((string)r["inStock"]);
curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"];
curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"];
}
}
return curr;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -