LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem creating multiple XML attributes with the same attribute name

Solved!
Go to solution

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

0 Kudos
Message 1 of 4
(5,450 Views)
Solution
Accepted by topic author TomB@NASIC

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);
}

Message 2 of 4
(5,423 Views)
Thanks Mohan.
Message Edited by Snowman 6008 on 11-05-2009 09:18 AM
Richard S -- National Instruments -- (former) Applications Engineer -- Data Acquisition with TestStand
0 Kudos
Message 3 of 4
(5,418 Views)
Thanks for the fix Mohan!  I didn't know you need to call the NewElement function each time you added an attribute.
0 Kudos
Message 4 of 4
(5,409 Views)