Accessing XML node single/multiple values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dahak

    Accessing XML node single/multiple values

    It seems that my GoogleFu has failed me tonight and I'm hoping I
    can find some advice.


    I've inherited a ASP.NET project written in Visual Basic.

    I'm not too familiar with the .NET framework or XML, so bear with
    me.

    The project is about 95% complete, but the XML parser needs quite
    a bit of tightening.

    One early problem I had was having the script crash when
    attempting to access an XmlTextReader node that didn't exist. I've
    since learned to check to see if the node IS NOTHING to prevent an
    actual read attempt on the non-existent node.

    My current troubles happen when trying to deal with a node that
    may - or may not - have multiple elements. For instance, there may be
    one or more //ShipTo/Address/PostalAddress/DeliverTo elements.

    I know that when there is _one_ element, I can access the node
    with doc.SelectSingl eNode(//Node/Path).InnerText .

    I know that when there is _more than one_ element, I can access
    the node with doc.SelectSingl eNode(//Node/Path).Item(x).I nnerText.

    But how do I properly test for the element node having a single or
    multiple values?

    I don't recall having any success with
    doc.SelectSingl eNode(//Node/Path).Item(0).I nnerText when the node
    wasn't actually a list.

    Any thoughts would be appreciated.


    Thanks.

    -Joe
  • \(O\)enone

    #2
    Re: Accessing XML node single/multiple values

    Dahak wrote:
    My current troubles happen when trying to deal with a node that
    may - or may not - have multiple elements. For instance, there may be
    one or more //ShipTo/Address/PostalAddress/DeliverTo elements.
    Have you tried using doc.SelectNodes instead of doc.SelectSingl eNode?

    This uses the same XPath syntax as SelectSingleNod e, but returns an
    XmlNodeList, which is basically a collection of all the matching nodes. You
    can then query the Count property to find how many nodes there were (zero,
    one or more are all possible return values here) and access the individual
    nodes using the default Item property.

    HTH,

    --

    (O)enone


    Comment

    • Dahak

      #3
      Re: Accessing XML node single/multiple values

      On Thu, 21 Feb 2008 09:32:27 -0000, an orbiting mind-control laser
      made "\(O\)enone " <oenone@nowhere .comwrite:
      >Dahak wrote:
      >My current troubles happen when trying to deal with a node that
      >may - or may not - have multiple elements. For instance, there may be
      >one or more //ShipTo/Address/PostalAddress/DeliverTo elements.
      >
      >Have you tried using doc.SelectNodes instead of doc.SelectSingl eNode?
      >
      >This uses the same XPath syntax as SelectSingleNod e, but returns an
      >XmlNodeList, which is basically a collection of all the matching nodes. You
      >can then query the Count property to find how many nodes there were (zero,
      >one or more are all possible return values here) and access the individual
      >nodes using the default Item property.
      >
      >HTH,
      I'll give that a whirl. I wasn't aware of how else the node data
      could be accessed.


      Like I said, VB.NET/XML isn't my thing, but the instances where I
      need to determine how to access the node values (0/1/many) have been
      causing the program to crash on me when an accessed node has more/less
      items than I am looking for.

      Thanks for the tip.

      -JPB

      Comment

      Working...