I am trying to parse on an xmlDoc to find all the companies with a specific postcode, or find out if there are none with that postcode. Ive written the code below which parses out all the companies with the postcode which works fine.
My problem is the situation where there are no companies that match the postcode. In this case, as I understand resultDoc is empty, I detect this as it throws a nullReference exception. This works, but I don't feel its very elegant to use exception catching to do this, and wondered if anyone could suggest a better way covering this. Or is this the only way to do this?
try
{
resultDoc.LoadXml(resultDoc.SelectSingleNode("descendant::company[address/postcode='" + postcode.Text + "']").OuterXml);
// POSTCODE Filter
if (resultDoc.SelectNodes("//company").Count == 1)
{
// narrowed it down to 1 so use
createRegistration(email.Text, resultDoc.SelectSingleNode("//companyID").InnerText);
);
} catch
{
// no record with postcode has not been found
}
My problem is the situation where there are no companies that match the postcode. In this case, as I understand resultDoc is empty, I detect this as it throws a nullReference exception. This works, but I don't feel its very elegant to use exception catching to do this, and wondered if anyone could suggest a better way covering this. Or is this the only way to do this?
try
{
resultDoc.LoadXml(resultDoc.SelectSingleNode("descendant::company[address/postcode='" + postcode.Text + "']").OuterXml);
// POSTCODE Filter
if (resultDoc.SelectNodes("//company").Count == 1)
{
// narrowed it down to 1 so use
createRegistration(email.Text, resultDoc.SelectSingleNode("//companyID").InnerText);
);
} catch
{
// no record with postcode has not been found
}