var qXML = (from nm in xelement.Descendants("EmployeeFinance")
where nm.Element("Status").Value.Contains(status) && int.Parse(nm.Element("Empersonal_Id").Value) == ele.EmpId
select nm).SingleOrDefault();
Above is my sample code. Now there are instances where ele.EmpId will be present in the xml and instance where it will not be. How do i handle the null exception that is thrown when ele.EmpId is not found.
Pravesh Singh
14-Aug-2014This checks both if the element is null and the element contains a non empty/null string value, and will short circuit if it fails either of those checks, before it tries to parse.
var qXML = (from nm in xelement.Descendants("EmployeeFinance")where nm.Element("Status").Value.Contains(status) &&
((nm.Element("Empersonal_Id") != null) &&
!string.IsNullOrEmpty(nm.Element("Empersonal_Id").Value) &&
int.Parse(nm.Element("Empersonal_Id").Value) == ele.EmpId)
select nm).SingleOrDefault();