XmlReader AppendChild does not add the same child value

XmlElement updateRecipient = doc.CreateElement("UpdateRecipient"); XmlElement email = doc.CreateElement("EMAIL"); XmlElement listID = doc.CreateElement("LIST_ID"); XmlElement column = doc.CreateElement("COLUMN"); XmlElement name = doc.CreateElement("NAME"); XmlElement value = doc.CreateElement("VALUE") root.AppendChild(body); body.AppendChild(updateRecipient); updateRecipient.AppendChild(listID); listID.InnerText = _listID; updateRecipient.AppendChild(email); email.InnerText = _email; updateRecipient.AppendChild(column); column.AppendChild(name); name.InnerText = _columnNameFrequency; column.AppendChild(value); value.InnerText = _actionID.ToString(); updateRecipient.AppendChild(column); column.AppendChild(name); name.InnerText = _columnNameStatus; column.AppendChild(value); 

for some reason, I get only one additional column instead of two under the updateRecipient element. I need them to appear in UpdateRecipient Node as follows:

 <UpdateRecipient> <LIST_ID>85628</LIST_ID> <EMAIL> somebody@domain.com </EMAIL> <COLUMN> <NAME>Frequency</NAME> <VALUE>1</VALUE> </COLUMN> <COLUMN> <NAME>Status</NAME> <VALUE>Opted In</VALUE> </COLUMN> </UpdateRecipient> 

but so far I get only one:

 <UpdateRecipient> <LIST_ID>85628</LIST_ID> <EMAIL> somebody@domain.com </EMAIL> <COLUMN> <NAME>Status</NAME> <VALUE>Opted In</VALUE> </COLUMN> </UpdateRecipient> 

When it falls into the first AppendChild (column), and then the name and value, the frequency shows find, but then is later redefined by status, and I want it to just add a new one under and I'm sure why it redefines and not adds another tag.

0
source share
3 answers

The problem is that you are reusing the variables "column", "name" and "value". You need to create new XmlElements for the second set.

+4
source

I do not know, but try to do it in reverse order. This is what I always did. Do not add updateRecipient to the root directory until you are done with it.

0
source

Thought you could reuse an existing item again, but you can't ..

0
source

Source: https://habr.com/ru/post/1343786/


All Articles