MyTechFinds.com

  • Increase font size
  • Default font size
  • Decrease font size

LINQ to XML

E-mail Print PDF
After I built the TRX to html report generator, there was a little obvious need to have it centrally available so that team members can upload the results to a central place and manager(s) and PMs can view them. The requirement was a web-based report viewer. I could have built it using the same old tool with some web-pages to give links to the reports, but I also wanted to take out the tester's dependency to have the html generator tool. I turned to develop a simple web-app to do the job. In the first apporach, I started like a newbie and started using the datagrid to display information out of result TRX. The upload part for easy and I am not going to talk about that here. I had to write quiet some code to parse the TRX again using XPATHs before I passed that to the datagrid to take care of the presentation. It was taking more time than what I was expecting, so I took a different approach, I turned to use LINQ to XML (basically to get away from parsing!)

LINQ to XML did the job, it saved my time from parsing XMLs and made it as easy as writing SQL queries against them. You will need 2 more namespaces

using System.Linq;
using System.Xml.Linq; 
 

Here are 2 small class that I wrote to get the data out of TRXs using LINQ, you can have functionality, this is just to give you an idea!


public class Result
{
private string TestId { get; set; }
public string Title { get; set; }
public string Outcome { get; set; }
private string TestListId { get; set; }

public static List<Result> GetResults()
{
XDocument doc = XDocument.Load(@"c:\test1.xml");

var res = from o in doc.Descendants("ManualTestResult")
select new Result
{
TestId = (string)o.Attribute("testId"),
Title = (string)o.Attribute("testName"),
Outcome = (string)o.Attribute("outcome"),
TestListId = (string)o.Attribute("testListId")
};

return res.ToList();
}

public static List<Result> GetResults(string outcome)
{
XDocument doc = XDocument.Load(@"c:\test1.xml");

var res = from o in doc.Descendants("ManualTestResult")
where (string)o.Attribute("outcome") == outcome
select new Result
{
TestId = (string)o.Attribute("testId"),
Title = (string)o.Attribute("testName"),
Outcome = (string)o.Attribute("outcome"),
TestListId = (string)o.Attribute("testListId")
};

return res.ToList();
}

public static List<string> GetOutcomes()
{
XDocument doc = XDocument.Load(@"c:\test1.xml");

var res = from o in doc.Descendants("ManualTestResult")
select (string)o.Attribute("outcome");

return res.Distinct().ToList();
}

}

public class Summary
{
public string Total { get; set; }
public string Executed { get; set; }
public string Passed { get; set; }
public string Failed { get; set; }

public static List<Summary> GetSummary()
{
XDocument doc = XDocument.Load(@"c:\test1.xml");

var res = from o in doc.Descendants("Counters")
select new Summary
{
Total = (string)o.Attribute("total"),
Executed = (string)o.Attribute("executed"),
Passed = (string)o.Attribute("passed"),
Failed = (string)o.Attribute("failed")
};

return res.ToList();
}

}


The output with some basic UI components looks like this,


Of course, what you see as test1.xml will be the trx file uploaded by the tester.

Here is presentation layer code (not including the aspx code as I have used all the default properties),

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Result.GetResults();
GridView1.DataBind();
GridView2.DataSource = Summary.GetSummary();
GridView2.DataBind();
Outcome.DataSource = Result.GetOutcomes();
Outcome.DataBind();
}

}

protected void FilterResults(object sender, EventArgs e)
{
GridView1.DataSource = Result.GetResults(Outcome.SelectedValue);
GridView1.DataBind();
}
}

( 0 Votes )
Comments
Search
Only registered users can write comments!

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 19 April 2010 18:03  

Our valuable member Ajay Majgaonkar has been with us since Thursday, 23 April 2009.

Show Other Articles Of This Author

Software Development

Login

Like it? Share it!


Search

Polls

Which of the following are characteristics of testable software?
 

MyTechFinds

Advertisement

Help us
We have 6 guests online