Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. How to Determine if element contains a given attribute

How to Determine if element contains a given attribute

Scheduled Pinned Locked Moved XML / XSL
tutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MStanbrook
    wrote on last edited by
    #1

    Code along the lines of: string value1 = currentNode.Attributes["value1"].Value; However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike Stanbrook

    J T 2 Replies Last reply
    0
    • M MStanbrook

      Code along the lines of: string value1 = currentNode.Attributes["value1"].Value; However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike Stanbrook

      J Offline
      J Offline
      Jeromy Stites
      wrote on last edited by
      #2

      IF NOT currentNode.Attributes.ItemOf("value1") IS NOTHING THEN value1 = currentNode.Attributes.Item("value1") END IF

      1 Reply Last reply
      0
      • M MStanbrook

        Code along the lines of: string value1 = currentNode.Attributes["value1"].Value; However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike Stanbrook

        T Offline
        T Offline
        Tim McCurdy
        wrote on last edited by
        #3

        There is an easier way by using XPath Queries.

        ...asuming you have an XMLDocument Open
        Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
        If (Not IsNothing(xNode)) Then
        'Do Something
        End If

        M 1 Reply Last reply
        0
        • T Tim McCurdy

          There is an easier way by using XPath Queries.

          ...asuming you have an XMLDocument Open
          Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
          If (Not IsNothing(xNode)) Then
          'Do Something
          End If

          M Offline
          M Offline
          MStanbrook
          wrote on last edited by
          #4

          Are you sure this is easier/more efficient that the method previously suggested, especially given that the current element node is already known and sotred in a local variable? ~Mike Stanbrook

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups