提取和更改 trx (xml) 文档中的数据
我有一个 .trx 文件(单元测试结果文件),里面只是 xml,我想读取该文件,比较一些标签并根据需要更改它们,然后再次保存文件。
我发现 VB.NET 有一些工具可以提供帮助,所以我做的第一件事就是将文档加载到 xml 文档中,这似乎工作正常,但我无法访问我需要的任何数据。现在我正在尝试访问计数器标签的属性并在重新运行一些测试后更改它们。
那么我该怎么做呢?
这会加载文件:
Dim Doc As XmlDocument = New XmlDocument
Doc.load("testFile.trx")
尝试访问节点的方法:
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本文档可以帮助您:
http://support.microsoft.com/kb/301225
另请参阅“Linq to xml”,这有很大帮助: http://www.aspfree.com/c/a/VB.NET/LINQ-to-XML-Programming-Using-Visual-BasicNET-2008/
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/
VB.Net 具有 XML Literals,这使得使用 XML 文档变得非常容易。下面的代码将为您提供
Counters
节点的total
属性:否则,这应该可以满足您的要求:
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 theCounters
node:Otherwise this should do what you're looking for: