Question

why isn't the innerText of my QUANTITY node isn't changing when I do this:

        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;
            }
        }

after I save my file the innerText of the selected QUANTITY node is still not changed. This is my XML file "Inventory.xml", where INVENTORY is the root:

This is a part of my XML file "Inventory.xml" where INVERNTORY is the root :

<?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>
Was it helpful?

Solution

the line device["QUANTITY"].InnerText.Replace(innerXml, newQuantity.ToString()); wont actually replace the InnerText. Instead, a new string is created here. Use something like

device["QUANTITY"].InnerText = newQuantity.ToString()

to set the InnerText to a new value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top