morcillo
morcillo

Reputation: 1109

Add xml child elements to specific parent node in C# with linq

I have an xml that goes like this:

<Equipment>
     <ModificationDate>
             <Data>
             </Data> 
     </ModificationDate>
</Equipment>

I need to add this sequence to the Data node:

<Table>
       <Name />
       <Value />
       <Number />
</Table>

The problem is that I need to add many Table nodes in the Data node. If iot was only one I would have finished it by now.

Whenever I try to add 1 table node with its elements, I do the first one correctly, but when I try to do the same to newer nodes i create the Table nodes but its elements all go to the first Table node, leaving the others empty.

That's why I wanted to know if anyone here knows how to add the new Table node and add the elements by using the Tabel index.

Or any other way, all I need to do is to add the elements to the specific Table node that I want, keeping in mind that they're all the same structure and keep the same name.

I've searched high and low and haven't come up with an answer

EDIT:

I'll try to explain better. I have the first xml and I need to add the second structure to the Data field. The problem is that the second structure will be repeated about 500 times inside Data.

I can add those 500 Table ... but when I try to add each number/value/name to its corresponding Table it doesn't work. I can only add number/value/name to the first Table created. That's why, in some way I need to access each Table separately and add the elements to it, so it will keep that structure. the final xml should look something like this:

<Equipment>
     <ModificationDate>
             <Data>
                  <Table>
                       <Name />
                       <Value />
                       <Number />
                  </Table>
                  <Table>
                       <Name />
                       <Value />
                       <Number />
                  </Table>
                  <Table>
                       <Name />
                       <Value />
                       <Number />
                  </Table>
             </Data> 
     </ModificationDate>
</Equipment>

But it ends up looking like this:

<Equipment>
     <ModificationDate>
             <Data>
                  <Table>
                       <Name />
                       <Value />
                       <Number />
                       <Name />
                       <Value />
                       <Number />
                       <Name />
                       <Value />
                       <Number />
                  </Table>
                  <Table />
                  <Table />
             </Data> 
     </ModificationDate>
</Equipment>

Please help me.

Upvotes: 1

Views: 1703

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499830

It's not entirely clear what you need, but you can always use this to get to a table element by index:

var table = doc.Root                         // Equipment
               .Element("ModificationData")  // ModificationData
               .Element("Data")              // Data
               .Elements("Table")
               .ElementAt(index);

// Now add to that element

On the other hand, if you're adding a new table node for each name/value/number, I don't see why you would need to access them by index...

Upvotes: 3

Related Questions