Link to home
Start Free TrialLog in
Avatar of mt600
mt600

asked on

Retrieving optional nodes from an XmlDocument

I have some Xml documents which i want to parse into C#, but they have many optional nodes that may or may not be there.
For example

<name><firstname>a</firstname><surname>b</surname></name>

or

<name><firstname>a</firstname><surname>c</surname><middlename>b</middlename></name>

How can i retrieve these single nodes quickly from Xml using c#?

I have been using .SelectSingleNode("//name/firstname") etc but these throw an exception if the node is not there.  Exceptions are pretty computationally expensive so i want to find a better way of finding out if a node exists in an xml document.

In summary, if i have an xml document which has lots of optional nodes, what is the best way to program my C# to use the values in these nodes if they exist.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Razzie_
Razzie_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mt600
mt600

ASKER

ok, thanks for your help.  It's good enough for me.