Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unlocking a column in excel using C# 17 28
pressing download button in c# 7 36
VB.net Filesystem watcher not working 5 38
T-SQL and CLR parameter strings 9 29
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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