471,593 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,593 software developers and data experts.

Searching node in XmlNode[] in c#

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
Nov 10 '08 #1
5 8150
Dormilich
8,656 Expert Mod 8TB
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
Nov 10 '08 #2
jkmyoung
2,057 Expert 2GB
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)
Nov 10 '08 #3
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
Nov 12 '08 #4
jkmyoung
2,057 Expert 2GB
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.
Nov 12 '08 #5
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']



@coolnags
Jan 19 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Rafaela K. Azinhal | last post: by
14 posts views Thread by neerajb | last post: by
1 post views Thread by John Wilhelm | last post: by
3 posts views Thread by Prashant Mahajan | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.