469,327 Members | 1,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Checking for existence of an attribute

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
5 19129


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
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
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
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
"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.

Similar topics

3 posts views Thread by Bob Roberts | last post: by
5 posts views Thread by Tongu? Yumruk | last post: by
4 posts views Thread by GujuBoy | last post: by
1 post views Thread by Xeno Campanoli | last post: by
2 posts views Thread by mike | last post: by
1 post views Thread by Vikas Rana | last post: by
4 posts views Thread by Patient Guy | last post: by
1 post views Thread by AndyB | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.