我有一个WinForm C#应用程序,允许服务员从餐馆接受客户订单。目前,从下图中可以看到输出menu.xml文件的datagridview。一旦服务员选择了一餐和一个座位,该信息就会传递到一个名为order.xml的新xml文件中。我已经完成了所有这些工作。
我需要帮助的是如何在订单提交后让程序减去库存。例如,当前数量为12,一旦我选择了餐1(鱼和薯条)并单击提交,我希望更新menu.xml文件以将数量显示为11.有人可以帮助我如何做到这一点
干杯。
我的menu.xml文件示例
<?xml version="1.0" encoding="utf-8"?>
<MenuInfo>
<Meal>
<MealID>1</MealID>
<Food>Meal 1 (Fish and Chips)</Food>
<Price>£4.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
<Meal>
<MealID>2</MealID>
<Food>Meal 2 (Chicken and Chips)</Food>
<Price>£3.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
<Meal>
<MealID>3</MealID>
<Food>Meal 3 (Saussage and Chips)</Food>
<Price>£2.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
<Meal>
<MealID>4</MealID>
<Food>Meal 4 (Burger and Chips)</Food>
<Price>£5.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
</MenuInfo>
****** ****** UPDATE
XDocument doc = XDocument.Load("order.xml");
XElement root = new XElement("MenuInfo");
foreach(DataGridViewRow dr in dataGridView.Rows)
{
if(dr.Selected)
{
root.Add(new XElement("Meal", dr.Cells["Food"].Value.ToString()));
root.Add(new XElement("SeatID", _seat));
root.Add(new XElement("TableID", buttonTable1.Text));
root.Add(new XElement("Price", dr.Cells["Price"].Value.ToString()));
doc.Element("Menu").Add(root);
doc.Save("order.xml");
}
}
这是我的代码,用于当用户将订单提交到&#34; order.xml&#34;文件。对困惑感到抱歉。这完全基于Linq to XML
答案 0 :(得分:0)
请看下面的代码 - 希望这是你想要的:
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<MenuInfo>
<Meal>
<MealID>1</MealID>
<Food>Meal 1 (Fish and Chips)</Food>
<Price>£4.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
</MenuInfo>");
XmlNode node = doc.DocumentElement.SelectSingleNode("/MenuInfo/Meal/Quantity");
int qty = Convert.ToInt32(node.InnerText);
// Deducting 1 from orginal quantity, you can use variable instead of quantity 1
node.InnerText = (qty - 1).ToString();
//Finally you can retrieve modified xml from using doc.InnerXml
string modifiedXml = doc.InnerXml
答案 1 :(得分:0)
假设您正在使用XmlDocument
,您可以执行以下操作:
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"
<MenuInfo>
<Meal>
<MealID>1</MealID>
<Food>Meal 1 (Fish and Chips)</Food>
<Price>£4.99</Price>
<Time>25 minutes</Time>
<Quantity>12</Quantity>
</Meal>
</MenuInfo>");
// this selects the correct MealID and updates the quantity all in one go
xml.SelectSingleNode("/MenuInfo/Meal[MealID = '1']/Quantity").InnerText = "11";