473,758 Members | 2,311 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 19538


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
23997
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
3008
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 and disadvantages but I will not discuss them now). Dynamic typing allows us to change types of...
4
5150
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
1490
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! http://www.eskimo.com/~xeno, xeno@eskimo.com, Xeno Campanoli
2
2043
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> function checkFile(frm)
1
3823
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
2384
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
2039
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
3328
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) len(sets.Set(FilteredList)): return False The first statement pulls the slice out of a matrix I need to check.
0
9299
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10076
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9908
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9740
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5175
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3832
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
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.