1

这是我的 XML 文件的一部分,我在我的 c# 表单上有一个组合框,其中包含设备的名称形成我使用 xpath 导航器放在那里的 xml 文件,加上一个数字向上向下,最后是一个按钮我打电话给买。我想要做的是当我点击按钮购买时,我希望 DEVICE 节点的 QUANTITY 节点值等于组合框 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>
4

3 回答 3

0

好吧,我认为在这种情况下迭代方法更容易理解。我们要做的是找到一个 XmlElement,它有一个指定的 Name 元素和所需的内容。

将 XmlDocument 视为一棵树会有所帮助。这棵树有不同的节点。最上面的节点是Inventory. TheInventory`-Node 有(在这种情况下)三个设备子节点。每个设备子节点也有子节点(名称、品牌等)。考虑到这一点,很容易找到数量。我们遍历树并检查名称元素是否与搜索字符串匹配。如果是,我们得到数量元素的值。这是一个小型控制台应用程序,说明了一种可能的解决方案。请记住,如果 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();
    }
于 2013-11-15T22:20:47.457 回答
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");
}
于 2013-11-15T22:35:13.353 回答
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;
                }
            }
于 2013-11-19T14:29:41.290 回答