提取和更改 trx (xml) 文档中的数据

发布于 2025-01-02 05:06:33 字数 1069 浏览 0 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2025-01-09 05:06:33

VB.Net 具有 XML Literals,这使得使用 XML 文档变得非常容易。下面的代码将为您提供 Counters 节点的 total 属性:

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)

否则,这应该可以满足您的要求:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文