You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中从XML的Attr num="123"块获取MC machine值?

在VB.NET中提取XML指定节点数据的最简方法

需求描述

我有多个XML文件,需要获取其中<MC>节点的machine和value属性值,这些<MC>节点均位于Attr num="123"的节点内部。此前未处理过XML文件,想知道VB.NET中实现该需求的最简单方法。

示例XML结构

<?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.radan.com/ns/rcd">
  <RadanAttributes>
    <Group class="system" name="System" desc="These attributes describe the RADAN system used to create this file."
        ord="8">
      <Attr num="13" name="Language" desc="RADAN language code." type="i" ord="1" value="19">
        <Valid perm="r"/>
      </Attr>
      <Attr num="15" name="Build" desc="RADAN software build." type="s" ord="3" value="2022.1.2228">
        <Valid perm="r"/>
      </Attr>
    </Group>
    <Group class="custom" name="Manufacturing" desc="These attributes are the manufacturing properties of the file."
        ord="6">
      <Attr num="119" name="Material" desc="Material." type="s" ord="1" value="Mild Steel">
        <Valid perm="e" max="100"/>
      </Attr>
      <Attr num="120" name="Thickness" desc="Thickness." type="r" ord="2" value="1">
        <Valid perm="e" min="0" max="99999"/>
      </Attr>
      <Attr num="121" name="Thickness units" desc="Thickness units." type="s" ord="4" value="mm">
        <Valid perm="e" expr="mm|in|swg" max="80"/>
      </Attr>
      <Attr num="123" name="Cycle time" desc="Cycle time in minutes." type="r" ord="26" value="0">
        <Valid perm="e" min="0"/>
        <MC machine="psys_CAA001_1" value="10"/>
        <MC machine="psys_CAA001_2" value="20"/>
        <MC machine="psys_CAA001_3" value="30"/>
        <MC machine="psys_CAA001_4" value="40"/>
      </Attr>
      <Attr num="124" name="Sheet X" desc="Sheet length in the X direction." type="r" ord="12" value="2500">
        <Valid perm="e" min="0"/>
      </Attr>
      <Attr num="125" name="Sheet Y" desc="Sheet length in the Y direction." type="r" ord="13" value="1250">
        <Valid perm="e" min="0"/>
      </Attr>
    </Group>
  </RadanAttributes>
</RadanCompoundDocument>

最简实现方法(使用LINQ to XML)

VB.NET中处理XML最简洁的方式是用LINQ to XML(XDocument类),代码逻辑清晰,适合快速查询指定节点。

步骤说明

  1. 导入必要的命名空间:System.Xml.Linq(处理XML)、System.IO(遍历文件)
  2. 指定XML命名空间:示例XML带有默认命名空间http://www.radan.com/ns/rcd,查询时必须指定,否则无法定位节点
  3. 遍历目标文件夹下的所有XML文件
  4. 对每个文件,加载为XDocument,通过LINQ查询提取目标数据
  5. 输出或保存提取到的结果

完整代码示例

Imports System.Xml.Linq
Imports System.IO

Module Module1
    Sub Main()
        ' 目标XML文件所在的文件夹路径
        Dim xmlFolderPath As String = "C:\YourXmlFilesFolder"
        ' XML默认命名空间,必须和目标XML中的xmlns一致
        Dim ns As XNamespace = "http://www.radan.com/ns/rcd"

        ' 遍历文件夹下所有.xml文件
        For Each xmlFilePath As String In Directory.GetFiles(xmlFolderPath, "*.xml")
            Try
                ' 加载XML文件
                Dim doc As XDocument = XDocument.Load(xmlFilePath)

                ' 提取Attr num="123"下的所有MC节点的machine和value属性
                Dim mcData = From attr In doc.Descendants(ns + "Attr")
                             Where attr.Attribute("num").Value = "123"
                             From mc In attr.Descendants(ns + "MC")
                             Select New With {
                                 .MachineName = mc.Attribute("machine").Value,
                                 .Value = mc.Attribute("value").Value
                             }

                ' 输出结果(也可以保存到文件/数据库)
                Console.WriteLine($"文件:{Path.GetFileName(xmlFilePath)}")
                For Each item In mcData
                    Console.WriteLine($"  机器:{item.MachineName},数值:{item.Value}")
                Next
                Console.WriteLine("------------------------")
            Catch ex As Exception
                ' 处理异常,比如文件损坏、格式错误等
                Console.WriteLine($"处理文件{Path.GetFileName(xmlFilePath)}出错:{ex.Message}")
            End Try
        Next

        Console.ReadKey()
    End Sub
End Module

关键说明

  • 命名空间处理:XML中的默认命名空间必须通过XNamespace指定,否则Descendants方法无法找到对应的节点
  • LINQ查询逻辑:先定位到num="123"的<Attr>节点,再提取其下所有<MC>节点的属性
  • 异常处理:添加Try-Catch块可以避免单个文件出错导致整个程序终止

内容的提问来源于stack exchange,提问作者Tribor83

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:10:30