/// <summary> /// writes to xml document /// </summary> /// <param name="doc"> xml document</param> public void SaveOrder(XmlDocument doc) { Product temp = new Product(); XmlNode root = doc.DocumentElement; XmlElement newID = doc.CreateElement("orderId"); XmlAttribute attrID = doc.CreateAttribute("name_id"); attrID.Value = Convert.ToString(this.id); newID.SetAttributeNode(attrID); root.InsertAfter(newID, root.LastChild); foreach (KeyValuePair <int, int> pair in this.products) { XmlElement newProduct = doc.CreateElement("Product"); XmlElement newId = doc.CreateElement("id"); XmlText identifier = doc.CreateTextNode(pair.Key.ToString()); newId.AppendChild(identifier); XmlElement newPrice = doc.CreateElement("price"); XmlText cost = doc.CreateTextNode((temp.PriceById(pair.Key) * pair.Value).ToString()); newPrice.AppendChild(cost); newProduct.AppendChild(newId); newProduct.AppendChild(newPrice); XmlElement newAmount = doc.CreateElement("amount"); XmlText amountStr = doc.CreateTextNode(pair.Value.ToString()); newAmount.AppendChild(amountStr); newProduct.AppendChild(newAmount); newProduct.AppendChild(newProduct); } }
/// <summary> /// writes to xml document /// </summary> /// <param name="doc"> xml document</param> public void SaveOrder(XmlDocument doc) { Product temp = new Product(); XmlNode root = doc.DocumentElement; XmlElement newID = doc.CreateElement("orderId"); XmlAttribute attrID = doc.CreateAttribute("name_id"); attrID.Value = Convert.ToString(this.id); newID.SetAttributeNode(attrID); root.InsertAfter(newID, root.LastChild); foreach (KeyValuePair<int, int> pair in this.products) { XmlElement newProduct = doc.CreateElement("Product"); XmlElement newId = doc.CreateElement("id"); XmlText identifier = doc.CreateTextNode(pair.Key.ToString()); newId.AppendChild(identifier); XmlElement newPrice = doc.CreateElement("price"); XmlText cost = doc.CreateTextNode((temp.PriceById(pair.Key)*pair.Value).ToString()); newPrice.AppendChild(cost); newProduct.AppendChild(newId); newProduct.AppendChild(newPrice); XmlElement newAmount = doc.CreateElement("amount"); XmlText amountStr = doc.CreateTextNode(pair.Value.ToString()); newAmount.AppendChild(amountStr); newProduct.AppendChild(newAmount); newProduct.AppendChild(newProduct); } }
/// <summary> /// Adds product to order /// </summary> /// <param name="id">id of product</param> public void AddProduct(int identifier) { Product temp = new Product(); XmlTextReader reader = new XmlTextReader(@"XMLFile1.xml"); temp.Price = temp.PriceById(identifier, reader); temp.Id = identifier; this.products.Add(temp); }