List Info

Thread: coding against missing xml nodes/attributes




coding against missing xml nodes/attributes
user name
2008-06-06 20:19:56
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

alex.smotritskygenzyme.com

 


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: coding against missing xml nodes/attributes
user name
2008-06-06 20:41:06
string myAttribute = null;
XmlAttribute someAttribute =
configNode.Attributes["someAttribute"];
if (someAttribute != null)
    myAttribute = someAttribute.Value;

Hope this helps,
//p

On Fri, Jun 6, 2008 at 9:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com> 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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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


Re: coding against missing xml nodes/attributes
user name
2008-06-06 20:41:06
string myAttribute = null;
XmlAttribute someAttribute =
configNode.Attributes["someAttribute"];
if (someAttribute != null)
    myAttribute = someAttribute.Value;

Hope this helps,
//p

On Fri, Jun 6, 2008 at 9:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com> 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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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


Re: coding against missing xml nodes/attributes
user name
2008-06-06 21:24:02
That worked, thanks a lot. Good to see these forums still
rock.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Paulo
Mouat
Sent: Friday, June 06, 2008 9:41 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] coding against missing xml
nodes/attributes

string myAttribute = null;
XmlAttribute someAttribute =
configNode.Attributes["someAttribute"];
if (someAttribute != null)
    myAttribute = someAttribute.Value;

Hope this helps,
//p

On Fri, Jun 6, 2008 at 9:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com> 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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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(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


Re: coding against missing xml nodes/attributes
user name
2008-06-06 21:29:14
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["someAttrib
ute"].Value))
{
    myAttribute =
configNode.Attributes["someAttribute"].Value;
}


-Pete

On Fri, Jun 6, 2008 at 8:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com>
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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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


Re: coding against missing xml nodes/attributes
user name
2008-06-06 21:24:02
That worked, thanks a lot. Good to see these forums still
rock.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Paulo
Mouat
Sent: Friday, June 06, 2008 9:41 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] coding against missing xml
nodes/attributes

string myAttribute = null;
XmlAttribute someAttribute =
configNode.Attributes["someAttribute"];
if (someAttribute != null)
    myAttribute = someAttribute.Value;

Hope this helps,
//p

On Fri, Jun 6, 2008 at 9:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com> 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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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(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


Re: coding against missing xml nodes/attributes
user name
2008-06-06 21:29:14
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["someAttrib
ute"].Value))
{
    myAttribute =
configNode.Attributes["someAttribute"].Value;
}


-Pete

On Fri, Jun 6, 2008 at 8:19 PM, Smotrisky, Alex
<Alex.Smotritskygenzyme.com>
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
>
> alex.smotritskygenzyme.com
>
>
>
>
> ===================================
> 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


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )