Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

Looping through XmlNodeList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raul - 20 Mar 2007 18:22 GMT
Hi,

I have the following code, which picks up 43 different nodes from my
XML document

XmlNodeList amortNodes = amortDoc.SelectNodes("//
TValueAmortizationSchedule/AmortizationLine");

each node looks like this:

<AmortizationLineType>8</AmortizationLineType>
<Date>09/01/2006</Date>
<Loan1Amount>100000000</Loan1Amount>
<Loan2Amount></Loan2Amount>
<Loan3Amount></Loan3Amount>
<Payment1Amount></Payment1Amount>
<Payment2Amount></Payment2Amount>
<Payment3Amount></Payment3Amount>
<InterestAccrued>0</InterestAccrued>
<InterestPaid>0</InterestPaid>
<PrincipalPaid>0</PrincipalPaid>
<UnpaidInterestBalance>0</UnpaidInterestBalance>
<PrincipalBalance>100000000</PrincipalBalance>
<TotalBalance>100000000</TotalBalance>
<RateChangeRate></RateChangeRate>
<RateChangeCompounding>13</RateChangeCompounding>

Now I am trying to loop through the node list to get get the nodes
with AmortizationLineType = 8

foreach (XmlNode amortNode in amortNodes)
{
    amortType = amortNode.SelectSingleNode("//AmortizationLine/
AmortizationLineType").InnerText;
    if (amortType.Equals("8"))
    {
    count++;

                ...........
   }
}

Now I know from my debugging, that not all 43 nodes fulfil this
criteria (ie amortType=8).  However, when I run this application, the
amortType always comes back as 8, which seems to tell me it is not
picking up the value?  Any ideas?

Thanks for your help.
Martin Honnen - 20 Mar 2007 18:35 GMT
> 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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.