XML - Getting an element value if another element is null from the same parent.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, first time using this forum so i will try to explain myself as good as i can.

I have the following sample schema:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; Something<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Country<span style="color:Blue; >IND<span style="color:Blue; </<span style="color:#A31515; Country<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; WorkDate<span style="color:Blue; >20110301<span style="color:Blue; </<span style="color:#A31515; WorkDate<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Code<span style="color:Blue; >P001<span style="color:Blue; </<span style="color:#A31515; Code<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Hours<span style="color:Blue; >1.00<span style="color:Blue; </<span style="color:#A31515; Hours<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Unit<span style="color:Blue; >H<span style="color:Blue; </<span style="color:#A31515; Unit<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Something<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Something<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Country<span style="color:Blue; >IND<span style="color:Blue; </<span style="color:#A31515; Country<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; WorkDate<span style="color:Blue; >20110302<span style="color:Blue; </<span style="color:#A31515; WorkDate<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Code <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Hours<span style="color:Blue; >6.50<span style="color:Blue; </<span style="color:#A31515; Hours<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Unit<span style="color:Blue; >H<span style="color:Blue; </<span style="color:#A31515; Unit<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Something<span style="color:Blue; >

[/code]

I have like 30 of those Something nodes per xml. What is check every Code element, and in case it is null (as you can see in the second Something node) get the value of the WorkDate element and Hours element.
I used XMLTextReader for pretty much everything related to xml reading, but as you can see in this case i need to go backwards in case i find a match (code == null). I know basically how XPath and XMLDocument works, but i cannot seem to find a way to implement
what i need.
I know i can just save every WorkDate in a "last_workdate" variable and keep on with XMLTextReader.Read(), but that is not an option, what i need is to go directly to the /Something node that contains a null Code value and get the WorkDate value.

I hope i explained myself, Thanks!

View the full article
 
Back
Top