473,573 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SelectSingleNod e 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.Attribut es("zipcode").V alue
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 19526


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 SelectSingleNod e 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.Attribut es("zipcode").V alue
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.HasAttr ibute("zipcode" )

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Thanks, I missed that.
"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:eN******** ******@TK2MSFTN GP09.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 SelectSingleNod e 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.Attribut es("zipcode").V alue
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.HasAttr ibute("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.co m> schrieb im Newsbeitrag
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
What's the proper technique for checking for the existence of an attribute
within a node?

Lets say I did a SelectSingleNod e 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.Attribut es("zipcode").V alue
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******** ***********@new s.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.co m> schrieb im Newsbeitrag
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
What's the proper technique for checking for the existence of an attribute within a node?

Lets say I did a SelectSingleNod e 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.Attribut es("zipcode").V alue
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.co m> wrote in message news:Oh******** ******@TK2MSFTN GP12.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
XmlAttributesCo llection.

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.HasAttribu tes )
{
XmlAttribute attr = elem.Attributes[ "city"];
if ( null != attr )
{
string city = attr.Value;
// Do something with city.
}
}

because it performs the look-up in the AttributesColle ction 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
23988
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
2990
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 - A Proposal As we all know, one of the best things about python and other scripting languages is dynamic typing (yes I know it has advantages...
4
5130
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
1480
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 protocol I can read as a newbie? Please can someone make a suggestion one what to read or try? Sincerely, Xeno -- Community FIRST, Then Trade!...
2
2028
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" onSubmit="return(validateData(this))"> <input type="file" name="txtFileToUpload"> <input type="submit" name="btnAdd" value="Add" class="form_button"> </form> ...
1
3815
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? Thanks, Vikas
4
2367
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 exceptions?
3
2034
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
3319
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 come up with so far: CheckList = for x in self.__XRList] FilteredList = filter((lambda x:x != 0),CheckList) if len(FilteredList)...
0
7992
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8190
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8048
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3722
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2183
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.