tags:

views:

54

answers:

3

Hi all,

I have some XML:

<metadata>
  <dataIdInfo>
    <idCitation>
      <resRefDate>
        <refDate>1996</refDate>
        <refDateTyp>
          <DateTypCd value="007" />
        </refDateTyp>
      </resRefDate>
      <resRefDate>
        <refDate>1998</refDate>
        <refDateTypCd>
          <DateTypCd value="003" />
        </refDateTypCd>
      </resRefDate>
    </idCitation>
  </dataIdInfo>
</metadata>

I'm trying to get the 1996 value from the key 007 so far I have got this

XmlDocument doc = new xmlDocument();
doc.LoadXml(myXmlString);  
XmlNode node = doc.SelectSingleNode("metadata/dataIdInfo/idCitation/resRefDate/refDate[refDateType/DateTypCd[@value=\"007\"]");

But I keep getting the error that the address has an invalid token. I've been reviewing the XPath example doc on msdn but obviously I have gone wrong in the square brackets part - help!

Thanks Rob

A: 

refDateTyp is not a subtag of refDate, which is indicated by your condition in the query.

you should probably replace ... /resRefDate/refDate[ ... by ... /resRefDate[ ...

Joachim VR
+6  A: 

Two problems.

Firstly you were failing to close both sets of square brackets, this is the invalid token.

Secondly, your filter expression (the square brackets) are in the wrong place, they should be before the refDate, as it is the resRefDate you are filtering. Below is the correct expression.

metadata/dataIdInfo/idCitation/resRefDate
                                [refDateTyp/DateTypCd/@value="007"]/refDate
Nick Jones
@Nick Jones: +1 Right answer. A minor typo in key value. Also, it's ok to break lines: it makes expression more clear.
Alejandro
That's it - thanks for your help!
rob
A: 

Looks like you have "refDateType" (with an "e" on the end) instead of "refDateTyp" which is what I see in your XML fragment.

Steve Michelotti