?
Solved

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

Posted on 2008-10-22
2
Medium Priority
?
12,433 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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