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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Reading xml

Reading xml

Scheduled Pinned Locked Moved C#
xmlhelp
5 Posts 2 Posters 1 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.
  • P Offline
    P Offline
    praveenkumar palla
    wrote on last edited by
    #1

    In my XML, for every node one optional attribute is there. I am trying to find that, specific node has that optional attribute or not at runtime but there are some issues Please help me on this Thanks in advance Praveenkumar Palla

    S 1 Reply Last reply
    0
    • P praveenkumar palla

      In my XML, for every node one optional attribute is there. I am trying to find that, specific node has that optional attribute or not at runtime but there are some issues Please help me on this Thanks in advance Praveenkumar Palla

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      What are the issues? You can't expect to get help if you don't identify the problem.

      Scott.


      —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

      P 2 Replies Last reply
      0
      • S Scott Dorman

        What are the issues? You can't expect to get help if you don't identify the problem.

        Scott.


        —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

        P Offline
        P Offline
        praveenkumar palla
        wrote on last edited by
        #3

        my xml is like this In the above xml there two child nodes for node "tabular" . In that one node has "pwd" attribute and another one don't have, it is optional one. So I want to know that for the specific node the attribute is there or not at runtime----I tried like this XmlDocument xmldoc2 = new XmlDocument(); xmldoc2.LoadXml(objXmlTasks.decrypt()); XmlNodeList xmlchildnodelst = xmldoc2.SelectNodes("/lasers/laser [@LaserName='" + laseridval + "']/properties/tabular/property[@name='" + childLaserSelectedName + "']"); Password= xmlchildnodelst[0].Attributes.GetNamedItem("pwd").Value; But there is now pwd attribute for second one so exception "object reference null" PraveenKumar Palla

        1 Reply Last reply
        0
        • S Scott Dorman

          What are the issues? You can't expect to get help if you don't identify the problem.

          Scott.


          —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

          P Offline
          P Offline
          praveenkumar palla
          wrote on last edited by
          #4

          my xml is like this In the above xml there two child nodes for node "tabular" . In that one node has "pwd" attribute and another one don't have, it is optional one. So I want to know that for the specific node the attribute is there or not at runtime----I tried like this XmlDocument xmldoc2 = new XmlDocument(); xmldoc2.LoadXml(objXmlTasks.decrypt()); XmlNodeList xmlchildnodelst = xmldoc2.SelectNodes("/lasers/laser [@LaserName='" + laseridval + "']/properties/tabular/property[@name='" + childLaserSelectedName + "']"); Password= xmlchildnodelst[0].Attributes.GetNamedItem("pwd").Value; But there is now pwd attribute for second one so exception "object reference null"

          S 1 Reply Last reply
          0
          • P praveenkumar palla

            my xml is like this In the above xml there two child nodes for node "tabular" . In that one node has "pwd" attribute and another one don't have, it is optional one. So I want to know that for the specific node the attribute is there or not at runtime----I tried like this XmlDocument xmldoc2 = new XmlDocument(); xmldoc2.LoadXml(objXmlTasks.decrypt()); XmlNodeList xmlchildnodelst = xmldoc2.SelectNodes("/lasers/laser [@LaserName='" + laseridval + "']/properties/tabular/property[@name='" + childLaserSelectedName + "']"); Password= xmlchildnodelst[0].Attributes.GetNamedItem("pwd").Value; But there is now pwd attribute for second one so exception "object reference null"

            S Offline
            S Offline
            Scott Dorman
            wrote on last edited by
            #5

            Yes, you can't do it this way. If GetNamedItem fails to find the attribute, it returns null, so the call to Value fails with the null reference exception. Instead, you need to do it like this:

            XmlNode node = xmlchildnodelst[0].Attributes.GetNamedItem("pwd");

            if (node != null)
            {
            // attribute exists, so retrieve the value
            Password = node.Value;
            }
            else
            {
            // attribute doesn't exist
            }

            Also, please put the code blocks, including the xml, inside <pre> tags, which will keep the formatting and make it much easier to read.

            Scott.


            —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

            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