Code:
#include "tinyxml2.h"
#include <cstdio>
int main() {
using tinyxml2::XMLDocument;
// Open XML Document
XMLDocument doc;
doc.LoadFile("hyperv.xml");
// Walk through the elements
auto *elemHyperV = doc.FirstChildElement("HyperV");
auto *elemMachine = elemHyperV->FirstChildElement("Machine");
auto *elemHardware = elemMachine->FirstChildElement("Hardware");
auto *elemGuestProperties = elemHardware->FirstChildElement("GuestProperties");
// Loop over all properties
for (auto *elem = elemGuestProperties->FirstChildElement("GuestProperty");
elem != nullptr; elem = elem->NextSiblingElement("GuestProperty")) {
const char *nameStr = nullptr;
elem->QueryStringAttribute("name", &nameStr);
int value;
elem->QueryAttribute("value", &value);
printf("Name: %s is %d\n", nameStr, value);
if (strcmp(nameStr, "resolution_height") == 0) {
// We've found resolution_height
// Update the value
elem->SetAttribute("value", 1337);
}
}
// Save file in the end
doc.SaveFile("UpdatedHyperV.xml");
return 0;
}