Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle optional elements when deserializing

Status
Not open for further replies.

msule

Programmer
Jul 15, 2005
3
US
Is there a efficient way to get the values from the XML with out throwing an "Object not set to an instance of an object" exception, when you have elements that may or may not be there. I know "if" statements would work, but that seems inefficient concerning the size of the xml documents.

Thank you
 
What values do you want to get and what you using?

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
We are using C# and what I ment by values is the data.
 
If the element or attribute is missing, you'll need to use an if{} statement to check for null. No real way around it.

A related question is: If an element or attribute is missing, what do I put for the data value? A good answer for that depends on what the app expects, but <type>.MinValue is sometimes a good value to use.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks, are problem was not checking for null, we were getting an exception because there was no object. We fixed this by initializing our objects in the class when we declared them. One of those, “slap yourself in the head”, mistakes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top