PC Review


Reply
Thread Tools Rate Thread

How do I loop through multiple instance of an XmlNode

 
 
Thirsty Traveler
Guest
Posts: n/a
 
      17th Jun 2006
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; }


 
Reply With Quote
 
 
 
 
Yoganathan Sivaram
Guest
Posts: n/a
 
      17th Jun 2006
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" <(E-Mail Removed)> wrote in message
news:%23Zff%(E-Mail Removed)...
> 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; }
>
>



 
Reply With Quote
 
Mike D Sutton
Guest
Posts: n/a
 
      17th Jun 2006
> 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: (E-Mail Removed)
WWW: Http://EDais.mvps.org/


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused - control not set to instance inside foreach loop - please help Alan Silver Microsoft ASP .NET 2 7th Nov 2005 11:11 PM
System.Xml.XmlNode to Office.Interop.Word.XMLNode farseer Microsoft C# .NET 0 17th Oct 2005 10:49 PM
xmlNode.InnerText vs. xmlNode.Value Dan Microsoft C# .NET 3 29th Aug 2004 02:26 AM
Object not set to an instance (ERROR) INDEXOF in do loop? jason@cyberpine.com Microsoft ASP .NET 2 6th Jun 2004 08:35 AM
Concatenating Object instance name in loop??? Isz Microsoft C# .NET 3 4th Jun 2004 02:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 AM.