0

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>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Aeropher
  • 117
  • 2
  • 4
  • 13

2 Answers2

0

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/

vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • Both links are dead for me. I know about Linq but what was the first link supposed to be about? ( I know its way back. I'm just asking in case. ) – Phillip Apr 27 '20 at 09:43
0

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)
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • My code still fell over at the following line, because Doc is empty. The xml literals works nicely though, how would you assign the xml from a file though? Dim attribute = Doc.SelectSingleNode("//TestRun/ResultSummary/Counters").Attributes("total").Value – Aeropher Feb 02 '12 at 15:45
  • In the above code, `X` is an `XElement` object. You can load it from disk by doing `XElement.Load(file)`. – Chris Haas Feb 02 '12 at 17:32