11-04-2009 10:07 AM
I am trying unsuccessfully trying to create multiple XML attributes with the same name as shown here in an example Microsoft configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key0" value="0" />
<add key="Key1" value="1" />
<add key="Key2" value="2" />
</appSettings>
</configuration>
I call the NewDocument, GetRootElement functions and then NewElement (appSetting), NewElement (add)
then I call AddAttribute with add, key & Key0 and again with add, value & 0. This seems to work correctly but when I try to add the second pair of attributes key & Key1 and value & 1 to element "add" it overwrites "<add key="Key0" value="0" />" and I end up with "<add key="Key1" value="1" />" only.
Am I doing something incorrectly or is CVI not capable of creating such an XML file?
Thanks
Solved! Go to Solution.
11-05-2009 08:37 AM
Here is how you do it. I am not showing error checking to keep things simple.
#include <cvixml.h>
static void CreateAddElement(CVIXMLElement parent, const char *key, const char *value)
{
CVIXMLElement add;
CVIXMLNewElement(parent, -1, "add", &add);
CVIXMLAddAttribute(add, "key", key);
CVIXMLAddAttribute(add, "value", value);
CVIXMLDiscardElement(add);
}
void main(void)
{
CVIXMLElement root, appSetting;
CVIXMLDocument doc;
CVIXMLNewDocument("configuration", &doc);
CVIXMLGetRootElement(doc, &root);
CVIXMLNewElement(root, -1, "appSetting", &appSetting);
CreateAddElement(appSetting, "Key0", "0");
CreateAddElement(appSetting, "Key1", "1");
CreateAddElement(appSetting, "Key2", "2");
CVIXMLDiscardElement(appSetting);
CVIXMLDiscardElement(root);
CVIXMLSaveDocument(doc, 1, "c:\\temp\\temp.xml");
CVIXMLDiscardDocument(doc);
}
11-05-2009 09:17 AM - edited 11-05-2009 09:18 AM
11-05-2009 10:01 AM