Linq to xml parsing and null check

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have an XML file which I need to parse and fetch a few nodes. Let me explain what I am trying to parse.
I have a test results file (TRX) which has some "Data Driven" test cases. Now I can identify the data driven test case node. Each test case has some stack info which I need to fetch. For the normal test cases, this is not an issue. But for Data driven test
cases, I have a bunch of "Trace" information which needs to be concatenated and fetched.
My usual node structure is

TestRun --> Results -- > UnitTestResult

From here I can check and compare for a test case name and fetch the trace info.

But for data driven test cases its a little different structure.

TestRun --> Results -- > UnitTestResult -- > InnerResults
Now each of the inner results will have something like
UnitTestResult --> Output -- > ErrorInfo --> StackTrace
Now there will be a bunch of these stack traces that I need to concatenate and fetch as a single "Trace". How do I do this?

This is the code that I use for parsing test cases which are not data driven.

<pre>Trace = (from testResult in trxDoc.Element(xns + "TestRun").Element(xns + "Results").Elements(xns + "UnitTestResult")
where
testResult.Attribute("testName").Value.Equals(testCaseName, StringComparison.CurrentCultureIgnoreCase) == true
select testResult.Element(xns + "Output").Element(xns + "<span style="white-space:normal StackTrace").Value).FirstOrDefault();
}[/code]
I was testing this piece of code for fetching the data driven test cases trace

<pre>Trace = (from testResult in trxDoc.Element(xns + "TestRun").Element(xns + "Results").Elements(xns + "UnitTestResult").Elements(xns + "InnerResults")
where
testResult!=null & testResult.Attribute("testName").Value.Equals(testCaseName, StringComparison.CurrentCultureIgnoreCase) == true

select testResult.Element(xns + "Output").Element(xns + "<span style="white-space:normal StackTrace").Value).FirstOrDefault();[/code]

But it throws up an object reference error.
What I was trying to do was to check for a null incase there is no "InnerResult", as it is in the case of the result node not being a data driven test case and then trying to get to the first reace.

Could anyone help me build the correct query please? I am pretty new to using LINQ.


Thanks and Regards.


View the full article
 
Back
Top