Enumerating XML Attribute Nodes

  • Thread starter Thread starter James
  • Start date Start date
J

James

Hi,

I am trying to optimize some c# code that contains lots of code such as

String name = null;
try
{
name = myXMLElement.Attributes["name"].Value;
}
catch
{
}


[OK - I admit it - It's my code!]

The code needs optimizing, and I suspect throwing exceptions like this is
time consuming. Is there a better way to determine whether attribute node is
present other than the (obvious) enumeration of all the attributes. Has
anybody done any profiling to determine whether this approach is just as
inefficient?

What I'm really looking for is a "HasAttribute" function

Apologies if this has been asked a thousand times before.

James
 
XmlElement has HasAttribute() and GetAttribute()... if you know your
XmlNode is an element, then cast to XmlElement and you're sorted.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top