删除 XML 文件中的子节点C++



我正在使用PugiXml库进行XML相关操作。

我的 XML 文件:

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

出于测试目的,我给出了一个硬编码值,我想删除带有属性Stamp = 14:50Time节点。

XML节点删除代码:我使用这个SO问题作为参考来删除节点(名称=Time和属性=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;
}

问:上面的代码运行没有任何错误。它甚至进入if-statement为什么原始XML文件在执行后保持不变?

PS:我确定根节点和XML文档通常可以正确读取,因为我能够在控制台上显示XML结构。

我认为问题出在保存上。

我在if-condition后执行了以下保存语句,它起作用了。

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

最新更新