Read trx file from C#

Posted by in C# category on for Intermediate level | Points: 250 | Views : 12037 red flag

Trx file is visual studio unit test result file extension in form of XML file.

 Download source code for Read trx file from C#


Trx file is visual studio unit test result file extension in XML format. Result of unit test is kept in TestResult folder in base directory. We can open these files in Visual Studio to see result. 


To understand parsing of trx file through C#.NET.

Programmatically parse trx file

Microsoft Visual Studio provides schema to read trx file. Schema is available in Visual Studio installation folder. It is named vstst.xsd and can be found under your Visual Studio installation directory. You can easily run the xsd.exe tool on this schema and generate the C# classes to parse the trx files.

Use command line to generate C# class from schema

Run below command in command prompt to generate C# class file:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\xsd.exe" "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Xml\Schemas\vstst.xsd" /c

Using Code


Code File

We can use ASP.NET GridView to display result. Create an ASP.NET Web Application and include vstst.cs file (we got after running xsd command mentioned above).

In the code snippet below, we are accessing base directory and iterating through all the files having trx extension. While iterating  “TestRunType” is used which is defined inside vstst.cs file. Here, a datatable is created at run time and retrieving all important information from UnitTestResultType. This datatable then used as a source for gridview. You can refer attached document to see full code.

public DataTable GetTestResultData()


            string fileName;

           DataTable testResultTable = null;



                //Construct DirectoryInfo for the folder path passed in as an argument

                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

                DirectoryInfo di = new DirectoryInfo(baseDirectory);

                ResultTable resultTable = new ResultTable();

                testResultTable = resultTable.CreateTestResultTable();

                // For each .trx file in the given folder process it

                foreach (FileInfo file in di.GetFiles("*.trx"))


                    fileName = file.Name;

                    //Deserialize TestRunType object from the trx file

                    StreamReader fileStreamReader = new StreamReader(file.FullName);

                    XmlSerializer xmlSer = new XmlSerializer(typeof(TestRunType));

                    TestRunType testRunType = (TestRunType)xmlSer.Deserialize(fileStreamReader);

                    //Navigate to UnitTestResultType object and update the sheet with test result //information

                    foreach(object itob1 in testRunType.Items)


                        ResultsType resultsType = itob1 as ResultsType;

                        if (resultsType != null)


                            foreach (object itob2 in resultsType.Items)


                                UnitTestResultType unitTestResultType = itob2 as UnitTestResultType;

                                if (unitTestResultType != null)


                                    DataRow row = testResultTable.NewRow();

                                    row[Constant.PROCESSEDFILENAME] = fileName;

                                    row[Constant.TESTID] = unitTestResultType.testId;

                                    row[Constant.TESTNAME] = unitTestResultType.testName;

                                    row[Constant.TESTOUTCOME] = unitTestResultType.outcome;

                                    row[Constant.ERRORMESSAGE] = ((System.Xml.XmlNode[])(((OutputType)(((TestResultType)(unitTestResultType)).Items[0])).ErrorInfo.Message))[0].Value;                                   








            catch (Exception ex)



            return testResultTable;



GridView  is used to bind all the columns for display. You can refer attached document to see full code.

<asp:GridView ID="grdTestResult" runat="server" CssClass="text"  AutoGenerateColumns="false" CellPadding="5"

            GridLines="Both" SelectedIndex="0">


<asp:BoundField DataField="ProcessedFileName" HeaderText="Processed File Name" />               

<asp:BoundField DataField="TestID" HeaderText="Test Run ID" />               

<asp:BoundField DataField="TestName" HeaderText="Test Name" />               

<asp:BoundField DataField="TestOutcome" HeaderText="Test Outcome" />

<asp:BoundField DataField="ErrorMessage" HeaderText="Error Message" />



Point to Note

If you have generated a fresh c# class file from schema, don’t forget to comment Items property of these two class:

1.       GenericTestType

2.       CodedWebTestElementType



With schema provided by the Microsoft it’s easy to parse trx file.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Prabhat Kumar
Member Level:
Member Status: Member
Member Since: 12/21/2012 5:30:10 AM
Country: India
Regards, Prabhat Kumar
Prabhat is a programmer and having 5+ years of developing experience in IT field. Major strength are C#, WPF, and SQL server 2008. He is also having good exposure in WCF.

Login to vote for this post.

Comments or Responses

Posted by: Budnet on: 1/30/2013 | Points: 25
good work

Login to post response

Comment using Facebook(Author doesn't get notification)