473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 19498


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Roberts | last post by:
I'm sure there must be a better way to do this: try: if item.page: DoSomething() except AttributError: pass Is there a simple way to check if item has "page" as one of its attributes?
5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
4
by: GujuBoy | last post by:
i want to check to see if a certain program is installed on my windows box using python. how can i do that...(ie, i want to see if "word" is installed) please help
1
by: Xeno Campanoli | last post by:
I'm having a hard time checking existence of windows. The only thing I found on this is page 224 of Rhino. Why is there no function to check this? Is there a publication on this part of the...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
1
by: Vikas Rana | last post by:
Hi, I am having trouble checking the existence of a temporary table. Neither of pg_tables or pg_class list the temporary tables. Is there any way to get the list of temporary tables? ...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
3
by: Mike-deerenews | last post by:
I would like to check for the existence of an attribute in order to prevent an exception from firing. Could someone provide an example? tia Mike
1
by: AndyB | last post by:
I have found a lot of material on removing duplicates from a list, but I am trying to find the most efficient way to just check for the existence of duplicates in a list. Here is the best I have...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.