read XML config into Visual C++ application

mamuttawa

New Member
I need to read this following XML in to some sort of structure to be used by my application for other functions.\[code\]<?xml version="1.0" encoding="UTF-8"?><profiles> <profile> <name>CAN-01</name> <fields> <field> <shortname>t</shortname> <longname>temperature</longname> </field> <field> <shortname>m_s</shortname> <longname>motor_status</longname> </field> </fields> </profile> <profile> <name>CAN-02</name> <fields> <field> <shortname>t</shortname> <longname>temperature</longname> </field> <field> <shortname>m_s</shortname> <longname>motor_status</longname> </field> <field> <shortname>m_d</shortname> <longname>motor_direction</longname> </field> </fields> </profile></profiles>\[/code\]I'm very new to C++ and this is the first Windows application I've ever written. I've been trying to use System::Xml \[code\]using namespace System::Xml;\[/code\] for the past couple of days and I'm getting no where. I can read first couple of nodes but then I'm unsure on how to correctly traverse the XML. I've attempted to use a third party library but I don't understand how to build in the additional libraries. Time is very tight so I need the quickest/easiest solution. Regarding how to store this data in the application should I create some models? For example a field model and a profile model, each field is added to it's relevant profile?Any help would be greatly appreciated.Here is the C++ Code I have been attempting to work with:void loadProfileConfig(){ XmlDocument ^ profileConfig = gcnew XmlDocument; String ^ profileFilename = L"profiles.xml"; if(File::Exists(profileFilename)) { XmlTextReader ^ reader = gcnew XmlTextReader(profileFilename); String ^ profileName; //move to profiles node reader->MoveToContent(); reader->Read(); do { switch (reader->NodeType) { case XmlNodeType::Element: // The node is an element. if(reader->Name == "profile") { reader->Read(); //this should be the profile name if(reader->Name == "name") { MessageBox::Show(reader->Value); } reader->Read(); //this should be the profile fields if(reader->Name == "fields") { reader->Read(); MessageBox::Show(reader->Name); do { reader->Read(); String ^ first = gcnew String(reader->Value); reader->Read(); String ^ second = gcnew String(reader->Value); MessageBox::Show("first: " + first + " second: " + second); reader->Read(); } while (reader->Name == "field"); } } break; case XmlNodeType::Text: //Display the text in each element. break; case XmlNodeType::EndElement: //Display the end of the element. //MessageBox::Show("Name + ">"); break; } } while (reader->Read()); } else { MessageBox::Show(L"The file " + profileFilename + L" was not found"); }}It doesn't work correctly because I have given up, this seems like a horrible way to read this config. Surely there is an easier/better way of doing this?
 
Back
Top