public List<Product> GetProductList() { List<Product> list = new List<Product>(); XmlTextReader reader = new XmlTextReader(dataFileName); reader.MoveToContent(); XmlDocument doc = new XmlDocument(); doc.LoadXml(reader.ReadOuterXml()); XmlNodeList nodes = doc.SelectNodes("products/product"); foreach (XmlNode node in nodes) { Product product = new Product(node.Attributes["id"].Value, node.Attributes["name"].Value, Convert.ToDouble(node.Attributes["price"].Value)); list.Add(product); } reader.Close(); return list; }
public Product GetProductByID(string anID) { Product product = null; XmlTextReader reader = new XmlTextReader(dataFileName); reader.MoveToContent(); XmlDocument doc = new XmlDocument(); doc.LoadXml(reader.ReadOuterXml()); XmlNode productNode = doc.SelectSingleNode("products/product[@id=" + anID + "]"); if (productNode != null) { product = new Product(productNode.Attributes["id"].Value, productNode.Attributes["name"].Value, Convert.ToDouble(productNode.Attributes["price"].Value)); } reader.Close(); return product; }
private void primitivesAddButton_Click(object sender, EventArgs e) { // Prompt the user to enter information about the product, // as well as properties to use when adding it to the cache if (this.enterNewItemForm.ShowDialog() == DialogResult.OK) { Product product = new Product( this.enterNewItemForm.ProductID, this.enterNewItemForm.ProductShortName, this.enterNewItemForm.ProductPrice); // Add the product to the cache, using the expiration and // priority settings according to what the user entered. switch (this.enterNewItemForm.Expiration) { case (ExpirationType.AbsoluteTime): primitivesCache.Add(product.ProductID, product, enterNewItemForm.Priority, new ProductCacheRefreshAction(), new AbsoluteTime(this.enterNewItemForm.AbsoluteTime)); break; case (ExpirationType.ExtendedFormat): primitivesCache.Add(product.ProductID, product, enterNewItemForm.Priority, new ProductCacheRefreshAction(), new ExtendedFormatTime("0 0 * * *")); break; case (ExpirationType.FileDependency): primitivesCache.Add(product.ProductID, product, enterNewItemForm.Priority, new ProductCacheRefreshAction(), new FileDependency("DependencyFile.txt")); break; case (ExpirationType.SlidingTime): primitivesCache.Add(product.ProductID, product, enterNewItemForm.Priority, new ProductCacheRefreshAction(), new SlidingTime(TimeSpan.FromMinutes(1))); break; } // Update the results text box to display information about the item just added this.primitivesResultsTextBox.Text += string.Format(Resources.Culture, Resources.AddItemToCacheMessage, product.ProductID, product.ProductName, product.ProductPrice, this.enterNewItemForm.Expiration.ToString(), this.enterNewItemForm.Priority.ToString()) + Environment.NewLine; AddScenarioSeparator(this.primitivesResultsTextBox); } }