📄 xmlcatalogprovider.cs
字号:
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>
///产品和分类的XML数据存
/// </summary>
public class XmlCatalogProvider : CatalogProvider
{
private string _xmlFile;
private string _xsdFile;
//构造函数,从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"];
//将文件路径映射为网站虚拟路径。
_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";
//调用Util静态公共类的ReadAndValiDateXml方法从Xml和Schema文件中读取
//XML数据并返回一个DataSet对象。
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中的数据赋值给Category对象。
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>对象
return list;
}
/// <summary>
/// 从一个指定的分类ID中返回产品列表
/// 如果分类ID为空,则返回一个空列表
/// </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);
//根据指定的分类ID查找产品索引。
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++;
}
//保存子产品ID号列表然后构造产品对象。
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>
/// 返回指定产品ID号的产品
///</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号不能为空。
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -