extracting and changing data in a trx (xml) document

976 views Asked by At

I have a .trx file (a unit test result file) which is just xml inside and I want to read the file compare a few of the tags and change them as necessary and save the file again.

I found that VB.NET has a few tools to help so the first thing I do is load the document into an xml document which seems to work fine but I can't access any of the data I need. Right now I'm trying to access the attributes of the counters tag and change them after rerunning some of the tests.

So how do I do it?

this loads the file:

Dim Doc As XmlDocument = New XmlDocument
Doc.load("testFile.trx")

Attempted ways to access node:

Dim attribute As Integer = CInt(xmlTrxMasterDoc.SelectSingleNode("/TestRun/ResultSummary/Counters").Attributes(i).InnerText)

Dim node As XmlNode = xmlTrxMasterDoc.SelectSingleNode("/Counters")
Dim i As Integer = 1
node.Attributes.Item(i).InnerText

XML

<?xml version="1.0" encoding="utf-8"?>
<TestRun someattributes="" >
    <ResultSummary outcome="Failed">
        <Counters total="115" executed="115" passed="110" error="0" failed="5" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
    </ResultSummary>
</TestRun>
2

There are 2 answers

1
vulkanino On BEST ANSWER

This document may help you: http://support.microsoft.com/kb/301225

Also take a look at "Linq to xml" that helps a lot: http://www.aspfree.com/c/a/VB.NET/LINQ-to-XML-Programming-Using-Visual-BasicNET-2008/

2
Chris Haas On

VB.Net has XML Literals which makes it really easy to work with XML documents. The code below will give you the total attribute of the Counters node:

Dim X = <TestRun someattributes="">
            <ResultSummary outcome="Failed">
                <Counters total="115" executed="115" passed="110" error="0" failed="5" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0"/>
            </ResultSummary>
        </TestRun>

Console.WriteLine(X.<ResultSummary>.<Counters>.@total)

Otherwise this should do what you're looking for:

Dim Doc As XmlDocument = New XmlDocument()
Doc.Load(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "testFile.trx"))
Dim attribute = Doc.SelectSingleNode("//TestRun/ResultSummary/Counters").Attributes("total").Value
Console.WriteLine(attribute)