Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中,如何用一个向上向下的数值替换/增加XML节点的值#_C#_Xml_Numericupdown - Fatal编程技术网

C# 在C中,如何用一个向上向下的数值替换/增加XML节点的值#

C# 在C中,如何用一个向上向下的数值替换/增加XML节点的值#,c#,xml,numericupdown,C#,Xml,Numericupdown,为什么我的数量节点的innerText在执行此操作时没有更改: XmlDocument inventory = new XmlDocument(); inventory.Load("Inventory.xml"); string vacuumName = (string)vacuumsBox.SelectedItem;//vacuumBox is a comboBox that contains the vacuums names

为什么我的数量节点的innerText在执行此操作时没有更改:

        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 number = Convert.ToInt32(vacuumsNumber.Value);//vacuumNumber is the name of the numeric up down
        int quantity, newQuantity = 0;
        foreach (XmlNode device in rootElement.ChildNodes)
        {
            if (String.Equals(device["NAME"].InnerText, vacuumName))
            {
                string innerXml = device["QUANTITY"].InnerText;
                quantity = Int32.Parse(device["QUANTITY"].InnerText);
                newQuantity = quantity + number;
                device["QUANTITY"].InnerText.Replace(innerXml, newQuantity.ToString()); 
                //device["QUANTITY"].InnerText.Insert(0, newQuantity.ToString());

                inventory.Save("Inventory.xml");
                break;
            }
        }
保存文件后,所选数量节点的内部文本仍然没有更改。 这是我的XML文件“Inventory.XML”,其中Inventory是根目录:

这是我的XML文件“Inventory.XML”的一部分,其中INVERNTORY是根目录:

<?xml version="1.0" encoding="utf-8"?>
<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>

空气导向无袋直立式
胡佛
UH72400
23
189.99
229.99
静动力袋装罐
胡佛
SH30050
18
299.99
334.99
绳状旋风棒真空
胡佛
SH20030
21
79.99
109.99
设备[“数量”].InnerText.Replace(innerXml,newQuantity.ToString())实际上不会替换内部文本。而是在此处创建一个新字符串。使用类似

device["QUANTITY"].InnerText = newQuantity.ToString()
将InnerText设置为新值