Connecting Tech Pros Worldwide Help | Site Map

Searching node in XmlNode[] in c#

Newbie
 
Join Date: Oct 2008
Posts: 10
#1: Nov 10 '08
I have an array of XmlNode[] which is returned after deserializing xml document. I have to search particlular XmlNode in the array based on the attribute "instrument" of Record XmlNode.

Sample node lookes like this
Expand|Select|Wrap|Line Numbers
  1. <Record instrument="VOD-US,USD,NORM,R">
  2.   <Field name="variant" type="2">R</Field> 
  3.   <Field name="hi" type="5">19.28</Field>   
  4. </Record>
  5.  
any help will be appreciated.

Regards,
Nags
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Nov 10 '08

re: Searching node in XmlNode[] in c#


XmlNode seems to be part of the implementation of DOM in C# (of which I don't know anything), so you could be able to use DOM methods to check the attribute.

sorry I can't be of more help

regards
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#3: Nov 10 '08

re: Searching node in XmlNode[] in c#


Use an iterator for the nodelist, something like:
Expand|Select|Wrap|Line Numbers
  1. foreach XmlNode node in the xmlNode[] {
  2.   XmlNode attr = node.Attributes.getNamedItem("instrument");
  3.   if (attr != null && attr.Value.Equals("VOD-US,USD,NORM,R")) 
  4.     do something
  5. } // end foreach
XMLNodeXmlNode Members (System.Xml)

XMLAttributeCollectionXmlAttributeCollection Members (System.Xml)
Newbie
 
Join Date: Oct 2008
Posts: 10
#4: Nov 12 '08

re: Searching node in XmlNode[] in c#


As of now I am doing in the same way as you specified. Here suppose I have 100 nodes I have to parse all the 100 nodes to get the last node. I was looking for something like we have predicate delegate.
Ex: Array.Find()

Is it possilbe in XmlArray[]

Nags
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#5: Nov 12 '08

re: Searching node in XmlNode[] in c#


It would be possible if the attribute was a direct property of the xmlNode. Since the attribute node is contained within each of the XMLNode lists, I don't know of a better way to search through the list. How are you getting this XMLNode []array, (or XMLNodeList?)

If you can, Change the way the list is formed to get the attribute nodes instead of the element nodes. Then when you find the right attribute node, just go to its parent to get the Record node.
Newbie
 
Join Date: Jan 2009
Posts: 1
#6: Jan 19 '09

re: Searching node in XmlNode[] in c#


XPath is query langueage for XML. Using this we can query xml file
Please search net for usage of XPath.

In your case following is the XPath query which serves your purpose.
/Record[@instrument='VOD-US,USD,NORM,R']



Quote:

Originally Posted by coolnags View Post

I have an array of XmlNode[] which is returned after deserializing xml document. I have to search particlular XmlNode in the array based on the attribute "instrument" of Record XmlNode.

Sample node lookes like this

Expand|Select|Wrap|Line Numbers
  1. <Record instrument="VOD-US,USD,NORM,R">
  2.   <Field name="variant" type="2">R</Field> 
  3.   <Field name="hi" type="5">19.28</Field>   
  4. </Record>
any help will be appreciated.

Regards,
Nags

Reply