473,563 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping through XmlNodeList

Hi,

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

XmlNodeList amortNodes = amortDoc.Select Nodes("//
TValueAmortizat ionSchedule/AmortizationLin e");

each node looks like this:

<AmortizationLi neType>8</AmortizationLin eType>
<Date>09/01/2006</Date>
<Loan1Amount>10 0000000</Loan1Amount>
<Loan2Amount> </Loan2Amount>
<Loan3Amount> </Loan3Amount>
<Payment1Amount ></Payment1Amount>
<Payment2Amount ></Payment2Amount>
<Payment3Amount ></Payment3Amount>
<InterestAccrue d>0</InterestAccrued >
<InterestPaid>0 </InterestPaid>
<PrincipalPaid> 0</PrincipalPaid>
<UnpaidInterest Balance>0</UnpaidInterestB alance>
<PrincipalBalan ce>100000000</PrincipalBalanc e>
<TotalBalance>1 00000000</TotalBalance>
<RateChangeRate ></RateChangeRate>
<RateChangeComp ounding>13</RateChangeCompo unding>

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

foreach (XmlNode amortNode in amortNodes)
{
amortType = amortNode.Selec tSingleNode("//AmortizationLin e/
AmortizationLin eType").InnerTe xt;
if (amortType.Equa ls("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.

Mar 20 '07 #1
6 27264
Raul wrote:
I have the following code, which picks up 43 different nodes from my
XML document

XmlNodeList amortNodes = amortDoc.Select Nodes("//
TValueAmortizat ionSchedule/AmortizationLin e");
foreach (XmlNode amortNode in amortNodes)
{
amortType = amortNode.Selec tSingleNode("//AmortizationLin e/
AmortizationLin eType").InnerTe xt;
You need/want a relative XPath expression here, relative to amortNode,
so use e.g.
amortType = amortNode.Selec tSingleNode("Am ortizationLineT ype").InnerText ;

If you use // at the beginning of an XPath expression then that is
always an absolute XPath selecting from the root downwards.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 20 '07 #2
On Mar 20, 1:35 pm, Martin Honnen <mahotr...@yaho o.dewrote:
You need/want a relative XPath expression here, relative to amortNode,
so use e.g.
amortType = amortNode.Selec tSingleNode("Am ortizationLineT ype").InnerText ;

If you use // at the beginning of an XPath expression then that is
always an absolute XPath selecting from the root downwards.
OHHHHhhhhhhhhhh hh, 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!

Mar 20 '07 #3
I'm curious, I am trying to make it pick out those with
AmortizationLin eType = 8 right from the XML, hence my node list would
only contain those nodes

So I try this...

amortType =
amortNode.Selec tSingleNode("Am ortizationLineT ype='8'").Inner Text;
But I get this error

System.Xml.XPat h.XPathExceptio n: The expression passed to this method
should result in a NodeSet

Mar 20 '07 #4
Abbas wrote:
I'm curious, I am trying to make it pick out those with
AmortizationLin eType = 8 right from the XML, hence my node list would
only contain those nodes

So I try this...

amortType =
amortNode.Selec tSingleNode("Am ortizationLineT ype='8'").Inner Text;
amortNode.Selec tSingleNode("Am ortizationLineT ype[. = '8']").InnerTex t
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 20 '07 #5
On Mar 20, 2:10 pm, Martin Honnen <mahotr...@yaho o.dewrote:
amortNode.Selec tSingleNode("Am ortizationLineT ype[. = '8']").InnerTex t
gives this error

System.NullRefe renceException: Object reference not set to an instance
of an object.

Mar 20 '07 #6
Abbas wrote:
On Mar 20, 2:10 pm, Martin Honnen <mahotr...@yaho o.dewrote:
> amortNode.Selec tSingleNode("Am ortizationLineT ype[. = '8']").InnerTex t

gives this error

System.NullRefe renceException: 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.Selec tSingleNode(
"AmortizationLi neType[. = '8']");
if (amortType != null) {
Console.WriteLi ne(amortType.In nerText);
}
else {
// no element found
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6857
by: Cheryl | last post by:
I need a sample that shows how can loop through the attributes and display the data.(.net / c#) <?xml version="1.0" encoding="UTF-8"?> <riders> <rider> <prename id=>Lance</prename> <surname>Armstrong</surname> </rider> <rider>
2
6473
by: Claire Reed | last post by:
Dear All, I am repeatedly encountering a problem whilst looping through XML Nodes and I am unsure as to what is going on and how I can get around it. I load the following XML document into an XmlDocument object using LoadXml: <?xml version="1.0" encoding="UTF-8"?>
1
9633
by: Peter Nofelt | last post by:
Hey All, I'm running into this issue with parsing through an xml document by tag name. Below is an example xml document: File Name: things.xml <things> <people> <name>Peter</name>
0
1502
by: Matthew.DelVecchio | last post by:
hello, i am attempting to do some databinding of an XmlNodeList to a DropDownList. id like to avoid looping thru my nodelist and hoped to do a simple .DataSource/.DataBind technique. for simplicity, lets say this is my xml: <people> <person name="Joe"></person>
3
5802
by: Maurice Cosgrave | last post by:
Hi, I was wondering if there was a way to move values from an array into an XmlNodeList? I have the values loaded into a string array from a previous point in the application; now I need to search the XML file for the items in the array and pull out the parent node. To do that I need an XmlNodeList. Cheers,
4
4611
by: SkyHook | last post by:
1. Under the topic "Select Nodes Using XPath Navigation" it says: "All XmlNodeList objects are synchronized with the underlying document, therefore if you ... modify the value of a node, that node is updated in the document it came from." 2. Under the topic "XmlNode.SelectNodes Method (String)" it says: "The XmlNodeList should not be...
1
7994
by: bigeddie | last post by:
Hi, I'm trying to get an instance off a XmlNodeList without using selectNodes(...) XmlNodeList test1 = new XmlNodeList(); (C#) but that doesn't work. What's my mistake?
4
7780
by: eggie5 | last post by:
Hi, I have an XmlNodeList and I need to reverse it. Just like Array.Reverse(), but it has to stay as an XmlNodeList. Any ideas?
1
2266
by: Raul | last post by:
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:
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.