Solved

c# XmlNode:Attributes.GetNamedItem - how to check for attribute exists or not???

Posted on 2008-10-22
2
12,364 Views
Last Modified: 2013-11-07
I have an xml document with 100 nodes, each node has 10-15 attribues.
I iterate through the nodes and try to read all the 15 attributes and store it...
If the attribute does not exists in the node, then
xNode.Attributes.GetNamedItem(sAttributeName).Value
throws an exception....

If all attributes are found, parse time is 0ms.
If I use a try-catch for handling the non existing attributes, the parse time is 3000ms, which is terrible...
If I use an IndexOf() for the attribute name in the node's text to check wether the attribute is found or not, then the parse time is 200ms. I think this is the overhead of the IndexOf string find operations...

The question is:
How to get an attribute (even if not exists) without this overhead?
In other words how to check for the non existence of an attribute???





public string GetSafeNodeAttribute(XmlNode xNode, string sAttributeName)
        {
            try
            {
                if (xNode.OuterXml.IndexOf(sAttributeName + "=\"") > -1)
                    return xNode.Attributes.GetNamedItem(sAttributeName).Value;
                else
                    return "";
            }
            catch (Exception ex)
            {
                //string sEx = ex.ToString();
                //Common.Trace(1,"Exception in CharmTabPage.GetSafeNodeAttribute\n" + ex.ToString());
            }
            return "";
        }

Open in new window

0
Comment
Question by:cosie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 250 total points
ID: 22780679
Hi,

Give this a try:

if (xNode.Attributes[sAttributeName] != null)
     return xNode.Attributes[sAttributeName].Value;
else
     return "";

Open in new window

0
 

Author Closing Comment

by:cosie
ID: 31508921
Great, thx...
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question