Multiple XML nodes with the same tag + DataRow access?

  • Thread starter Thread starter jamie
  • Start date Start date
J

jamie

hello,

I realize that this is an ADO.Net question, but I'm having now luck with
responses.

I have the following xml structure which I load into a DataRow (the xml
document is loaded into a dataset...)

<EXMAPLE_NODE>
<A>string1</A>
<B>string2</B>
<C>string3</C>
<D>string4_1</D>
<D>string4_2</D>
<D>string4_3</D>
</EXMAPLE_NODE>


I know I can Access nodes A,B &C using indexed access. How can I access
the D nodes (I don't want unique names for them)?

Is there anyway to access these via a foreach iteration?

Thank you in advance,

Jamie
 
Jamie,

You have a few options here. The first would be to use a for loop,
incrementing an integer value and then using that as the index to access the
columns. As with mostly every collection in .NET, it starts at zero.

If you want to use a foreach, you can't do it with the DataRow class,
because it doesn't implement IEnumerable. However, you could use a foreach
on the Columns collection of the Table that the data row belongs to, and
then get the column value from the data row with the current column.
However, this requires more code than just cycling through the values with
an integer indexer.

Hope this helps.
 
Nicholas said:
However, you could use a foreach
on the Columns collection of the Table that the data row belongs to, and
then get the column value from the data row with the current column.

Thanks Nicholas, I will give this a try...

One more question if you don't mind (I actually wrote the XML wrong):

<EXMAPLE_NODE>
<A>string1</A>
<B>string2</B>
<C>string3</C>
<SUB_SECTION>
<D>string4_1</D>
<D>string4_2</D>
<D>string4_3</D>
<SUB_SECTION>
</EXMAPLE_NODE>

Now for this layout I can iterate with a foreach loop on the DataTable
"EXAMPLE_NODE" but this will only work for A,B & C... I can not reach
the D nodes because they are in another table. I've tried to use a
foreach loop for "SUB_SECTION" but this will not work if there are more
than 1 SUB_SECTION nodes in the tree, it will return all D nodes.

thanks for the help.

Jamie
 
Jamie,

If you want only the SUB_SECTION elements (or rather, rows in the other
table that correspond to the current rows), then you will have to set up a
DataRelation that links the two tables together, and then get the related
rows based on the row that you are on in the parent table. You can do this
by calling the GetChildRows method on the DataRow.

Hope this helps.
 
Nicholas said:
Jamie,

If you want only the SUB_SECTION elements (or rather, rows in the other
table that correspond to the current rows), then you will have to set up a
DataRelation that links the two tables together, and then get the related
rows based on the row that you are on in the parent table. You can do this
by calling the GetChildRows method on the DataRow.

Hope this helps.
Thanks again Nicholas :-)
 
Back
Top