> I have the following code, which picks up 43 different nodes from my
> XML document
>
> XmlNodeList amortNodes = amortDoc.SelectNodes("//
> TValueAmortizationSchedule/AmortizationLine");
> foreach (XmlNode amortNode in amortNodes)
> {
> amortType = amortNode.SelectSingleNode("//AmortizationLine/
> AmortizationLineType").InnerText;
You need/want a relative XPath expression here, relative to amortNode,
so use e.g.
amortType = amortNode.SelectSingleNode("AmortizationLineType").InnerText;
If you use // at the beginning of an XPath expression then that is
always an absolute XPath selecting from the root downwards.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Abbas - 20 Mar 2007 19:04 GMT
> You need/want a relative XPath expression here, relative to amortNode,
> so use e.g.
> amortType = amortNode.SelectSingleNode("AmortizationLineType").InnerText;
>
> If you use // at the beginning of an XPath expression then that is
> always an absolute XPath selecting from the root downwards.
OHHHHhhhhhhhhhhhh, I didnt know that! I'm still getting used to XML
and XPath so I didn't know that the // it would make a difference
Cheers!
Abbas - 20 Mar 2007 19:07 GMT
I'm curious, I am trying to make it pick out those with
AmortizationLineType = 8 right from the XML, hence my node list would
only contain those nodes
So I try this...
amortType =
amortNode.SelectSingleNode("AmortizationLineType='8'").InnerText;
But I get this error
System.Xml.XPath.XPathException: The expression passed to this method
should result in a NodeSet
Martin Honnen - 20 Mar 2007 19:10 GMT
> I'm curious, I am trying to make it pick out those with
> AmortizationLineType = 8 right from the XML, hence my node list would
[quoted text clipped - 4 lines]
> amortType =
> amortNode.SelectSingleNode("AmortizationLineType='8'").InnerText;
amortNode.SelectSingleNode("AmortizationLineType[. = '8']").InnerText

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Abbas - 20 Mar 2007 19:29 GMT
> amortNode.SelectSingleNode("AmortizationLineType[. = '8']").InnerText
gives this error
System.NullReferenceException: Object reference not set to an instance
of an object.
Martin Honnen - 21 Mar 2007 13:24 GMT
>> amortNode.SelectSingleNode("AmortizationLineType[. = '8']").InnerText
>
> gives this error
>
> System.NullReferenceException: Object reference not set to an instance
> of an object.
Right, as you are looping through elements that might not meet the
condition you need to break that up e.g.
XmlNode amortType = amortNode.SelectSingleNode(
"AmortizationLineType[. = '8']");
if (amortType != null) {
Console.WriteLine(amortType.InnerText);
}
else {
// no element found
}

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/