I am getting a response in form of a SOAP message from a web service. I don’t know on beforehand what nodes are available (some but not all).
Lets say I get data about a customer
Name
City
and in the code I can write
string name = "";
string city = "";
name = customer.name;
city = customer.city;
If the city returns an empty string I can handle that with writing
city = (string)customer.city;
instead. But sometimes the response doesn’t include a city node and then I get the NullReferenceException was unhandled error, how can I fix this?
Are you actually asking for this:
?
Incidentally, casting a
stringto astringas you have here:(string)""(the equiavlent of(string)customer.Citywhencustomer.City == "") is not necessary. (Unless of coursecustomer.Cityis somehow actually not astring.)