这是我的XML文件的一部分,我在我的c#表单上有一个组合框,其中包含xml文件中的设备名称,我使用xpath导航器,加上数字向上,以及最后一个叫做买的按钮。 我想要做的是当我按下按钮时我想要DEVICE节点的QUANTITY节点值,其中NAME节点值等于组合框SelectedValue增加数字上下值的数量。 换句话说,如何选择DEVICE元素的QUANTITY节点,该元素的NAME等于组合框中的名称,并使用C#将数值增加到数值上。
<INVENTORY>
<DEVICE ID="1">
<NAME>Air Steerable Bagless Upright</NAME>
<BRAND>Hoover</BRAND>
<MODEL>UH72400</MODEL>
<QUANTITY>23</QUANTITY>
<BUYING_PRICE>189.99</BUYING_PRICE>
<SELLING_PRICE>229.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID="2">
<NAME>Quietforce Bagged Canister</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH30050</MODEL>
<QUANTITY>18</QUANTITY>
<BUYING_PRICE>299.99</BUYING_PRICE>
<SELLING_PRICE>334.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID="3">
<NAME>Corded Cyclonic Stick Vacuum</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH20030</MODEL>
<QUANTITY>21</QUANTITY>
<BUYING_PRICE>79.99</BUYING_PRICE>
<SELLING_PRICE>109.99</SELLING_PRICE>
</DEVICE>
答案 0 :(得分:0)
嗯,我认为在这种情况下,迭代方法更容易理解。我们要做的是找到一个XmlElement,它具有一个具有所需内容的指定Name元素。
将XmlDocument视为树是有帮助的。这棵树有不同的节点。最顶层的节点是Inventory. The
库存`节点(在这种情况下)具有三个设备子节点。每个设备子节点也有子节点(名称,品牌等)。考虑到这一点,很容易找到数量。我们遍历树并检查name元素是否与搜索字符串匹配。如果是,我们得到数量元素的值。这是一个小型控制台应用程序,说明了可能的解请记住,如果XML格式不正确(例如缺少NAME元素),此解决方案可能会导致异常。
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
string searchName = "Quietforce Bagged Canister";
xdoc.LoadXml(@"<INVENTORY>
<DEVICE ID='1'>
<NAME>Air Steerable Bagless Upright</NAME>
<BRAND>Hoover</BRAND>
<MODEL>UH72400</MODEL>
<QUANTITY>23</QUANTITY>
<BUYING_PRICE>189.99</BUYING_PRICE>
<SELLING_PRICE>229.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID='2'>
<NAME>Quietforce Bagged Canister</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH30050</MODEL>
<QUANTITY>18</QUANTITY>
<BUYING_PRICE>299.99</BUYING_PRICE>
<SELLING_PRICE>334.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID='3'>
<NAME>Corded Cyclonic Stick Vacuum</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH20030</MODEL>
<QUANTITY>21</QUANTITY>
<BUYING_PRICE>79.99</BUYING_PRICE>
<SELLING_PRICE>109.99</SELLING_PRICE>
</DEVICE></INVENTORY>");
//this is the INVENTORY element. You might need to customize that, in case the INVENTORY is not the root
XmlNode rootElement = xdoc.FirstChild;
//-1 means that no mathing item was found.
int quantity = -1;
//Here we iterate over every device element
foreach(XmlNode device in rootElement.ChildNodes)
{
//TODO: Validate XML first. Missing Elements can cause exceptions
//We can access the child elements of the decives with their element name
if(String.Equals(device["NAME"].InnerText, searchName))
{
quantity = Int32.Parse(device["QUANTITY"].InnerText);
break;
}
}
Console.WriteLine(quantity.ToString());
Console.ReadLine();
}
答案 1 :(得分:0)
如果您更喜欢使用LINQ to SQL方法,那么这样做会起作用。
public string GetXML()
{
return @"<root><DEVICE ID=""1"">
<NAME>Air Steerable Bagless Upright</NAME>
<BRAND>Hoover</BRAND>
<MODEL>UH72400</MODEL>
<QUANTITY>23</QUANTITY>
<BUYING_PRICE>189.99</BUYING_PRICE>
<SELLING_PRICE>229.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID=""2"">
<NAME>Quietforce Bagged Canister</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH30050</MODEL>
<QUANTITY>18</QUANTITY>
<BUYING_PRICE>299.99</BUYING_PRICE>
<SELLING_PRICE>334.99</SELLING_PRICE>
</DEVICE>
<DEVICE ID=""3"">
<NAME>Corded Cyclonic Stick Vacuum</NAME>
<BRAND>Hoover</BRAND>
<MODEL>SH20030</MODEL>
<QUANTITY>21</QUANTITY>
<BUYING_PRICE>79.99</BUYING_PRICE>
<SELLING_PRICE>109.99</SELLING_PRICE>
</DEVICE></root>";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
///this would be your up down control
var xml = XDocument.Parse(GetXML());
var device = xml.Descendants("DEVICE").Where(d => d.Descendants("NAME").First().Value == "Air Steerable Bagless Upright");
var quantity = Convert.ToInt16(device.Descendants("QUANTITY").First().Value);
quantity++;
device.Descendants("QUANTITY").First().Value = quantity.ToString();
xml.Save(@"c:\temp\temp.xml");
}
答案 2 :(得分:0)
private void button2_Click(object sender, EventArgs e)//Button Buy clicking method
{
XmlDocument inventory = new XmlDocument();
inventory.Load("Inventory.xml");
string vacuumName = (string)vacuumsBox.SelectedItem;//vacuumBox is a comboBox that contains the vacuums names
XmlNode rootElement = inventory.FirstChild.NextSibling;//first child is the xml encoding type tag not the root
int quantity, newQuantity = 0;
foreach (XmlNode device in rootElement.ChildNodes)
{
if (String.Equals(device["NAME"].InnerText, vacuumName))
{
int number = Convert.ToInt32(vacuumsNumber.Value);//vacuumNumber is the name of the numeric up down
quantity = Int32.Parse(device["QUANTITY"].InnerText);
newQuantity = quantity + number;
device["QUANTITY"].InnerText = newQuantity.ToString();//Updating the QUANTITY node value.
inventory.Save("Inventory.xml");
continue;
}
}