c++xmlpugixml

Removing child node in a XML file C++


I am using PugiXml library for XML related operations.

My XML File:

<CurrentStatus>
    <Time Stamp= "12:30">
        <price>100</price>
        <amount>1</amount>
    </Time>

    <Time Stamp= "14:50">
        <price>10</price>
        <amount>5</amount>
    </Time> 

    <Time Stamp= "16:30">
        <price>10</price>
        <amount>5</amount>
    </Time>     
</CurrentStatus>

For testing purpose, I am giving a hard-coded value that I want to delete the node Time with attribute Stamp = 14:50.

XML Node Removal Code: I used this SO Question as reference to remove a node (name = Time and Attribute = 14:50).

for (xml_node child = doc.child("CurrentStatus").first_child(); child; )
{
    xml_node next = child.next_sibling();       
    string attributeValue = child.attribute("Stamp").as_string();

    if (attributeValue == "14:50")
    {
        cout << "delete" << endl;
        child.parent().remove_child(child);
    }

    child = next;
}

Question: The above code runs without any error. It even enters into the if-statement but why the original XML file remains same after the execution?

PS: I am sure that the root node and the XML document is getting read properly in General since I am able to display the XML structure on the console.


Solution

  • I think that the problem was in Saving.

    I executed the following save statement after the if-condition and it worked.

    doc.save_file("D:/myfile.xml");