473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I loop through multiple instance of an XmlNode

Given the XML sampe below, how would I loop through all of the borrowers in
the XML document in order to process the attributes:

<REQUEST_GROUP MISMOVersionID="2.1.1" InternalAccountIdentifier="">

<REQUEST RequestDatetime="2003-12-08T09:37:28>

- <REQUEST_DATA>

<BORROWER _FirstName="BNUM1" _LastName="LNAME1" />

- <BORROWER _FirstName="BNUM2" _LastName="LNAME2" />

</REQUEST_DATA>

</REQUEST>

</REQUEST_GROUP>

// In this example, I would like to process the BORROWER attributes for both
borrowers... how?

node = xml.SelectSingleNode("REQUEST_GROUP/REQUEST/REQUEST_DATA/BORROWER",
nsMgr);
if (node != null) { fn = node.Attributes["_FirstName"].Value; }

if (node != null) { ln = node.Attributes["_LastName"].Value; }
Jun 17 '06 #1
2 6636
Hi Thirsty,

I assume you meant to process both BORROWER elements (as they not
attributes, but elements). Following is a modified version of your code

XmlNodeList nodeList =
xml.SelectNodes("REQUEST_GROUP/REQUEST/REQUEST_DATA/BORROWER");

foreach (XmlNode node in nodeList)
{
String fn = node.Attributes["_FirstName"].Value;
String ln = node.Attributes["_LastName"].Value;
MessageBox.Show(fn);
}

Hope this helps!

Y. Sivaram
"Thirsty Traveler" <nf*@nospam.com> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Given the XML sampe below, how would I loop through all of the borrowers
in the XML document in order to process the attributes:

<REQUEST_GROUP MISMOVersionID="2.1.1" InternalAccountIdentifier="">

<REQUEST RequestDatetime="2003-12-08T09:37:28>

- <REQUEST_DATA>

<BORROWER _FirstName="BNUM1" _LastName="LNAME1" />

- <BORROWER _FirstName="BNUM2" _LastName="LNAME2" />

</REQUEST_DATA>

</REQUEST>

</REQUEST_GROUP>

// In this example, I would like to process the BORROWER attributes for
both borrowers... how?

node = xml.SelectSingleNode("REQUEST_GROUP/REQUEST/REQUEST_DATA/BORROWER",
nsMgr);
if (node != null) { fn = node.Attributes["_FirstName"].Value; }

if (node != null) { ln = node.Attributes["_LastName"].Value; }

Jun 17 '06 #2
> I assume you meant to process both BORROWER elements (as they not attributes, but elements). Following is a modified
version of your code

XmlNodeList nodeList = xml.SelectNodes("REQUEST_GROUP/REQUEST/REQUEST_DATA/BORROWER");

foreach (XmlNode node in nodeList)
{
String fn = node.Attributes["_FirstName"].Value;
String ln = node.Attributes["_LastName"].Value;
MessageBox.Show(fn);
}


This would cause a problem if the required attribute was not present since Attributes["_FirstName"] would return null.
You can get around this problem by slightly modifying the XPath query to pull back only those nodes that have the
required attribute:

***
XmlNodeList nodeList =
xml.SelectNodes("REQUEST_GROUP/REQUEST/REQUEST_DATA/BORROWER[@_FirstName]");
***

Of course if the attribute is required in your schema then this makes no difference so either version will work in that
case.
Hope this helps,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jun 17 '06 #3

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

Similar topics

4
2510
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
47
12035
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
1
5860
by: DraguVaso | last post by:
Hi, I found some examples for storing the FormSettings of a Form in an XML-file, but none of these could match my criteria: What I am looking for is the possibility to save the FormSettings of...
0
1073
by: GeorgeAtkins | last post by:
VS 2003 using VB.NET I'm writing a program that reads text entries from a one file and matches them against values in an XML document. However, the code seems to just sit in the loop and never get...
9
6138
by: Art | last post by:
Hello, Here is a fragment of my code, which fails and I don't know why. --------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(@"C:\mydoc.xml"); foreach (XmlNode objNode...
5
2130
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
0
1303
by: mlafarlett | last post by:
Geez...seems like i've compared every friggin character...works n this programA but not n programB. I have a config file with multiple sections and when trying to read each section, it works great...
3
1415
by: daz_oldham | last post by:
Hi Everyone I was wondering if anyone knew of a good guide to working with multiple namespaces under .NET? And also help me with my latest poser. As you may have calculated by my other posts,...
2
5200
by: DragonLord | last post by:
Ok basically this is mostly working, What i want to do is create an xml file with multiple updates that have the same attribute. <root> <update> <element1>value</element1> ...
0
7226
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,...
0
7125
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...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
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...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
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...
0
1561
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 ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.