Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

827 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