By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,979 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,979 IT Pros & Developers. It's quick & easy.

Checking for existence of an attribute

P: n/a
What's the proper technique for checking for the existence of an attribute
within a node?

Lets say I did a SelectSingleNode which returned this element:

<AnAddress city="San Francisco" state="CA" />

What's the best why of determining if the attribute zipcode exists in this
node? If I try accessing the attribute with code like:
ZipCode = myNode.Attributes("zipcode").Value
I'll get an run-time error if the attribute doesn't exists. I know that I
can try accessing the attribute within a try-catch block and catch the
exception that way. Or, I could enumerate through all of the node's
attributes first to determine if that attribute exists.

Just wondering if there's a better solution? I didn't see any method like
"Exists"...
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Richard L Rosenheim wrote:
What's the proper technique for checking for the existence of an attribute
within a node?

Lets say I did a SelectSingleNode which returned this element:

<AnAddress city="San Francisco" state="CA" />

What's the best why of determining if the attribute zipcode exists in this
node? If I try accessing the attribute with code like:
ZipCode = myNode.Attributes("zipcode").Value
I'll get an run-time error if the attribute doesn't exists. I know that I
can try accessing the attribute within a try-catch block and catch the
exception that way. Or, I could enumerate through all of the node's
attributes first to determine if that attribute exists.

Just wondering if there's a better solution? I didn't see any method like
"Exists"...


You can check
element.HasAttribute("zipcode")

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

P: n/a
Thanks, I missed that.
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...


Richard L Rosenheim wrote:
What's the proper technique for checking for the existence of an attribute within a node?

Lets say I did a SelectSingleNode which returned this element:

<AnAddress city="San Francisco" state="CA" />

What's the best why of determining if the attribute zipcode exists in this node? If I try accessing the attribute with code like:
ZipCode = myNode.Attributes("zipcode").Value
I'll get an run-time error if the attribute doesn't exists. I know that I can try accessing the attribute within a try-catch block and catch the
exception that way. Or, I could enumerate through all of the node's
attributes first to determine if that attribute exists.

Just wondering if there's a better solution? I didn't see any method like "Exists"...


You can check
element.HasAttribute("zipcode")

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3

P: n/a
I don't know if it is the best solution, but...

if (nd.Attributes["key"] != null)
{
string y = nd.Attributes["key"].Value;
}

seems to work

"Richard L Rosenheim" <ri*****@rlr.com> schrieb im Newsbeitrag
news:%2***************@tk2msftngp13.phx.gbl...
What's the proper technique for checking for the existence of an attribute
within a node?

Lets say I did a SelectSingleNode which returned this element:

<AnAddress city="San Francisco" state="CA" />

What's the best why of determining if the attribute zipcode exists in this
node? If I try accessing the attribute with code like:
ZipCode = myNode.Attributes("zipcode").Value
I'll get an run-time error if the attribute doesn't exists. I know that I
can try accessing the attribute within a try-catch block and catch the
exception that way. Or, I could enumerate through all of the node's
attributes first to determine if that attribute exists.

Just wondering if there's a better solution? I didn't see any method like
"Exists"...

Nov 12 '05 #4

P: n/a
Thanks for the reply and the tip. Another person pointed out that
XMLElement has a HasAttribute method, which allows you to check for the
existence of an attribute. For whatever reason, XMLNode does not have that
method...

Richard
"Albert Greinöcker" <al****************@uibk.ac.at> wrote in message
news:O5*******************@news.chello.at...
I don't know if it is the best solution, but...

if (nd.Attributes["key"] != null)
{
string y = nd.Attributes["key"].Value;
}

seems to work

"Richard L Rosenheim" <ri*****@rlr.com> schrieb im Newsbeitrag
news:%2***************@tk2msftngp13.phx.gbl...
What's the proper technique for checking for the existence of an attribute within a node?

Lets say I did a SelectSingleNode which returned this element:

<AnAddress city="San Francisco" state="CA" />

What's the best why of determining if the attribute zipcode exists in this node? If I try accessing the attribute with code like:
ZipCode = myNode.Attributes("zipcode").Value
I'll get an run-time error if the attribute doesn't exists. I know that I can try accessing the attribute within a try-catch block and catch the
exception that way. Or, I could enumerate through all of the node's
attributes first to determine if that attribute exists.

Just wondering if there's a better solution? I didn't see any method like "Exists"...


Nov 12 '05 #5

P: n/a
"Richard L Rosenheim" <ri*****@rlr.com> wrote in message news:Oh**************@TK2MSFTNGP12.phx.gbl...
Thanks for the reply and the tip. Another person pointed out that
XMLElement has a HasAttribute method, which allows you to check for the
existence of an attribute. For whatever reason, XMLNode does not have that
method...


Hi Richard,

The reason XmlNode lacks a HasAttribute method is because XmlNode is
also the base class several for other DOM node types like the XmlComment
and XmlWhitespace.

When you think about it, comments and whitespace never seem to have
attributes, so HasAttribute( ) would always be false. :-)

Another option not mentioned is that you can call GetNamedItem on the
XmlAttributesCollection.

On the matter of HasAttribute( ) from XmlElement, I'd really recommend
using HasAttributes instead as being the best solution:

// Use HasAttributes then GRAB it, instead of HasAttribute( ).
if ( elem.HasAttributes )
{
XmlAttribute attr = elem.Attributes[ "city"];
if ( null != attr )
{
string city = attr.Value;
// Do something with city.
}
}

because it performs the look-up in the AttributesCollection only once, and
makes the XmlAttribute available to you for caching to make later accesses
more efficient.

If you can avoid it, don't look in the Attributes property if you don't have to
(i.e., when an elem absolutely has no attributes) because the XmlAttributes-
Collection is lazily created on-demand.
Derek Harmon
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.