1

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.

1 Answer 1

3

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...

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try that tomorrow morning. I just left my work and the code is there. Yes I need top add a new table for each number/name/value. The probem is when I try to add that new value/number/name it just goes to the first Table i created. The others remain empty
@morcillo: Well it's impossible to say what you're doing wrong without seeing the code, really. Please read tinyurl.com/so-hints for suggestions about how to write questions which are easier to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.