473,399 Members | 4,177 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,399 software developers and data experts.

Finding out if attribute X exists with XmlTextReader

My docs have attributes called "order", with a default value of 1. So if
there is no "order" attribute, the program shall use the value 1.
Implementing this with XmlTextReader was harder than expected. Since
GetAttribute() returns null if the attribute isn't found, and null can
be interpreted as false, I thought this would do it:

decimal order = 1;
if (r.GetAttribute("order"))
order = Convert.ToDecimal(r.GetAttribute("order"));

I get the error:

Cannot implicitly convert type 'string' to 'bool'

What's the right way to do it?

Gustaf
Jan 24 '06 #1
1 3985
Gustaf,
The GetAttribute method returns the attribute as string value or null if no
attribute exists. The following should test this condition correctly.
String sAttr = r.GetAttribute("order");
if(sAttr != null)
{
order = Convert.ToDecimal(sAttr);
}
else
{
//do whatever you want when the attribute doesn't exist
}
Good luck...
"Gustaf" <gu*****@algonet.se> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
My docs have attributes called "order", with a default value of 1. So if
there is no "order" attribute, the program shall use the value 1.
Implementing this with XmlTextReader was harder than expected. Since
GetAttribute() returns null if the attribute isn't found, and null can be
interpreted as false, I thought this would do it:

decimal order = 1;
if (r.GetAttribute("order"))
order = Convert.ToDecimal(r.GetAttribute("order"));

I get the error:

Cannot implicitly convert type 'string' to 'bool'

What's the right way to do it?

Gustaf

Jan 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard L Rosenheim | last post by:
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"...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
1
by: soupaman | last post by:
Im trying to output some filtered xml using the xmlTextReader I know the code and commenting needs cleaned up and eventually plan to add the values to a dataset. currently what this is doing is...
1
by: techie | last post by:
Hi, I am using an xml schema (Schema1.xsd) which refers to two other Schemas as follows. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:plcm-s="http://www.mycompany.com/plcm"...
3
by: Saurabh | last post by:
Hi, my xml is: <ROOT> <FILE NAME="filename">CDATA</FILE> </ROOT> this xml is argument to a function in C#. i want to get the filename in a string variable. how do i do it???...
3
by: Goran Djuranovic | last post by:
Hi all, I ran into a problem where my XMLTextReader fails on .Read() when I have "<" character in one of the attribute's values. What I am trying to do is replace illegal characters ("<", "&" ,...
1
by: lejason | last post by:
Hi, I have an XML file that is a list of product models and info. The list will eventually contain about 100 products, each product having multiple elements for things like names,...
1
by: selvi166 | last post by:
hi, i am having an textbox which displays the xml code. i want to read the specific attribute value and i need to save that richtextbox content with that value as filename. for example, my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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,...
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...
0
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...
0
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...
0
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,...

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.