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. how to know there is no required attribute for the childnode in xml file

how to know there is no required attribute for the childnode in xml file

Scheduled Pinned Locked Moved C#
csharpxmltutorial
4 Posts 3 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

    I am using an xml and in that childnodes have one attribute which is optional, so that attribute may be present or not. in front-end from c#.net 2005 i am reading that attribute, when it is not present it giving exception. so how to find that there is no attribute for that childnode in runtime. Thanks in advance PraveenKumar Palla

    M A 2 Replies Last reply
    0
    • P praveenkumar palla

      I am using an xml and in that childnodes have one attribute which is optional, so that attribute may be present or not. in front-end from c#.net 2005 i am reading that attribute, when it is not present it giving exception. so how to find that there is no attribute for that childnode in runtime. Thanks in advance PraveenKumar Palla

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, I think Reflection is what you are looking for. The way from the object to the Attributes would be like this.

      //For searching of property Attributes
      using System.Reflection;

      Type t = yourObject.GetType();
      foreach (PropertyInfo pi in t.GetProperties())
      {
      if(pi.PropertyType.Attributes & TypeAttributes.Serializable) //For excample a comparison to the Serializable Attribute.
      {

      }
      }

      //For Searching of CustomAttributes of your object.
      using System.Reflection;

      Type t = yourObject.GetType();
      object[] AllAttributes = t.GetCustomAttributes(true); //For excample

      Hope it helps! All the best, Martin

      1 Reply Last reply
      0
      • P praveenkumar palla

        I am using an xml and in that childnodes have one attribute which is optional, so that attribute may be present or not. in front-end from c#.net 2005 i am reading that attribute, when it is not present it giving exception. so how to find that there is no attribute for that childnode in runtime. Thanks in advance PraveenKumar Palla

        A Offline
        A Offline
        andre_swnpl
        wrote on last edited by
        #3

        just test for null. eg. XmlDocument oXML = new XmlDocument(); oXML.LoadXml("<root><name id='1' value='ggg' value2='ttt'/><name id='2' value='erer'/></root>"); XmlNodeList nl = oXML.SelectNodes("//name"); foreach (XmlNode n in nl) { if (n.Attributes["value2"] != null) { //Do Something } }

        P 1 Reply Last reply
        0
        • A andre_swnpl

          just test for null. eg. XmlDocument oXML = new XmlDocument(); oXML.LoadXml("<root><name id='1' value='ggg' value2='ttt'/><name id='2' value='erer'/></root>"); XmlNodeList nl = oXML.SelectNodes("//name"); foreach (XmlNode n in nl) { if (n.Attributes["value2"] != null) { //Do Something } }

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

          when the below statement is executed then only getting exception(i.e. focusing to catch()) if (n.Attributes["value2"] != null)

          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