Searching node in XmlNode[] in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolnags
    New Member
    • Oct 2008
    • 10

    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
    Code:
    <Record instrument="VOD-US,USD,NORM,R">
      <Field name="variant" type="2">R</Field> 
      <Field name="hi" type="5">19.28</Field>   
    </Record>
    any help will be appreciated.

    Regards,
    Nags
    Last edited by Dormilich; Jan 19 '09, 05:47 PM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

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

      XMLAttributeCol lectionXmlAttributeCol lection Members (System.Xml)
      Last edited by Dormilich; Jan 19 '09, 05:48 PM. Reason: added [code] tags

      Comment

      • coolnags
        New Member
        • Oct 2008
        • 10

        #4
        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

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          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.

          Comment

          • raghukv
            New Member
            • Jan 2009
            • 1

            #6
            Why dont you use XPath

            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='VO D-US,USD,NORM,R']



            Originally posted by coolnags
            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
            Code:
            <Record instrument="VOD-US,USD,NORM,R">
              <Field name="variant" type="2">R</Field> 
              <Field name="hi" type="5">19.28</Field>   
            </Record>
            any help will be appreciated.

            Regards,
            Nags
            Last edited by Dormilich; Jan 19 '09, 05:50 PM. Reason: fixed XPath

            Comment

            Working...