<?xml version="1.0" encoding="UTF-8"?>
<Pit>
<ROW ExecutionID="1366617710" Date="2011-11-09 00:04:04.303" AssertionName="Check for critical conditions" />
<ROW ExecutionID="1366619608" Date="2011-11-09 00:04:16.893" AssertionName="Check for critical conditions" />
</Pit>
I am trying to retrieve the date value based on a certain executionID.
I tried using the below query, but I reach an “exception”. It’s fairly simple but dunno why it fails.
The exception message is Object reference not set to an instance of an object.
public static string GetRowError(XDocument xmlDoc, string executionID)
{
string resultType = string.Empty;
try
{
resultType = (from testResult in xmlDoc.Elements("Pit")
where
testResult != null &&
testResult.Attribute("ExecutionID").Value.Equals(executionID, StringComparison.CurrentCultureIgnoreCase) == true
select testResult.Attribute("Date").Value).FirstOrDefault();
}
catch (Exception ex)
{
resultType = ex.ToString();
}
return resultType;
}
will return the
<Pit>elements, the rest off your logic assumes they are<ROW>elements.Either use
xmlDoc.Elements("Pit").Elements()or directly selectxmlDoc.Descendants("ROW").Your error is caused by: