Solved

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

Posted on 2008-10-22
2
12,212 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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