string myAttribute = String.Empty; if (configNode.Attributes["someAttribute"] != null) { myAttribute = configNode.Attributes["someAttribute"].Value; } or to be safer you could use:
if (configNode.Attributes["someAttribute"] != null && !String.IsNullOrEmpty(configNode.Attributes["someAttribute"].Value)) { myAttribute = configNode.Attributes["someAttribute"].Value; } -Pete On Fri, Jun 6, 2008 at 8:19 PM, Smotrisky, Alex <[EMAIL PROTECTED]> wrote: > I've got an object called configNode of type > System.Configuration.ConfigXmlElement. > > To read data from a config file I have code such as this: > > string myAttribute = configNode.Attributes["someAttribute"].Value; > > If the attribute is missing or incorrectly named then I get a > System.NullReferenceException. > > Is there a way to check for attribute existence and such? Using try > catch for this seems more like a last resort then the right way to write > the code. > > > > Thanks, > > > > Alex Smotritsky > > > > > > Thanks, > > > > Alex Smotritsky > > 212-698-0394 > > [EMAIL PROTECTED] > > > > > =================================== > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com