📄 productstable.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
/// <summary>
/// Products - singleton pattern to avoid multiple retrievals from xml file
/// </summary>
public class ProductsTable: System.Data.DataTable
{
private static ProductsTable _oneInstance = null;
private static System.DateTime _snapshotTime = DateTime.Now;
public static ProductsTable Instance()
{
if(_oneInstance == null)
{
_oneInstance = new ProductsTable();
}
//redo every 2 minutes
if (Math.Abs(System.DateTime.Now.Minute - ProductsTable._snapshotTime.Minute) > 2)
{
_oneInstance = new ProductsTable();
}
return _oneInstance;
}
private ProductsTable()
{
_snapshotTime = DateTime.Now;
ReloadTable();
}
public void ReloadTable()
{
_snapshotTime = DateTime.Now;
this.Clear();
//DataTable Schema
DataColumn myPrimaryKeyCol = new DataColumn();
myPrimaryKeyCol.DataType = System.Type.GetType("System.Int32");
myPrimaryKeyCol.ColumnName = "product_id";
myPrimaryKeyCol.ReadOnly = true;
myPrimaryKeyCol.Unique = true;
this.Columns.Add(myPrimaryKeyCol);
this.Columns.Add("name", System.Type.GetType("System.String"));
this.Columns.Add("price", System.Type.GetType("System.Decimal"));
this.Columns.Add("shipping", System.Type.GetType("System.Decimal"));
this.Columns.Add("handling", System.Type.GetType("System.Decimal"));
this.Columns.Add("picture", System.Type.GetType("System.String"));
this.Columns.Add("description", System.Type.GetType("System.String"));
this.Columns.Add("taxable", System.Type.GetType("System.Boolean"));
DataColumn[] myPrimaryKeyColumns = new DataColumn[1];
myPrimaryKeyColumns[0] = this.Columns["product_id"];
this.PrimaryKey = myPrimaryKeyColumns;
//GET xml Data
string xmlPath = System.Web.Configuration.WebConfigurationManager.AppSettings["contentPath"] + "Products.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodeLst; // current nodelist working with
nodeLst = doc.GetElementsByTagName("product");
foreach (XmlNode node in nodeLst)
{
DataRow dr = this.NewRow();
dr["product_id"] = int.Parse(node.ChildNodes[0].InnerText);
dr["name"] = node.ChildNodes[1].InnerText;
dr["price"] = decimal.Parse(node.ChildNodes[2].InnerText);
dr["shipping"] = decimal.Parse(node.ChildNodes[3].InnerText);
dr["handling"] = decimal.Parse(node.ChildNodes[4].InnerText);
dr["picture"] = node.ChildNodes[5].InnerText;
dr["description"] = node.ChildNodes[6].InnerText;
dr["taxable"] = bool.Parse(node.ChildNodes[7].InnerText);
this.Rows.Add(dr);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -