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

如何用LINQ结合XPath根据属性值修改Feature标签属性

问题:根据XML中同级name属性值修改driver属性

原始XML内容:

<?xml version="1.0"?>
<report>
    <feature tag="Config"/>
    <feature tag="Runtime">
        <feature tag="Metadata">
            <property name="date" value="16.01.2025"/>
            <property name="time" value="09:31:34"/>
        </feature>
        <feature tag="Templates">
            <feature tag="Template">
                <property name="username" value="myself"/>
                <property name="password" value="something"/>
                <feature tag="Data sources">
                    <feature tag="Source">
                        <property name="name" value="modules"/>
                        <property name="driver" value="eval"/>
                    </feature>
                    <feature tag="Source">
                        <property name="name" value="Artifact"/>
                        <property name="driver" value="eval"/>
                    </feature>
                    <feature tag="Source">
                        <property name="name" value="Comments"/>
                        <property name="driver" value="eval"/>
                    </feature>
                </feature>
            </feature>
        </feature>
    </feature>
</report>

现有VB.NET代码(已能提取Source标签,但缺少修改逻辑):

Private Function ModifyXml(ByVal xml As String) As Boolean
     Try
         Dim xdoc As New XDocument
         xdoc = XDocument.Parse(xml)

         Dim query As String = "/report/feature[@tag='Runtime']/feature[@tag='Templates']/feature[@tag='Template']/feature[@tag='Data sources']/feature[@tag='Source']"
         xdoc.XPathSelectElements(query).ToList()

         xdoc.Save("c:\temp\myFile.xml")

         Return True
     Catch ex As Exception
         Return False
     End Try
 End Function

修改后的完整代码

遍历每个Source节点,根据同级name属性值匹配设置driver的属性值:

Private Function ModifyXml(ByVal xml As String) As Boolean
    Try
        Dim xdoc As XDocument = XDocument.Parse(xml)
        Dim query As String = "/report/feature[@tag='Runtime']/feature[@tag='Templates']/feature[@tag='Template']/feature[@tag='Data sources']/feature[@tag='Source']"
        
        ' 遍历所有目标Source节点
        For Each sourceNode As XElement In xdoc.XPathSelectElements(query)
            ' 获取当前节点下的name属性值
            Dim nameValue As String = sourceNode.Elements("property") _
                .FirstOrDefault(Function(p) p.Attribute("name").Value = "name")? _
                .Attribute("value").Value
            
            ' 定位当前节点下的driver属性节点
            Dim driverProperty As XElement = sourceNode.Elements("property") _
                .FirstOrDefault(Function(p) p.Attribute("name").Value = "driver")
            
            If driverProperty IsNot Nothing Then
                ' 根据name值设置对应的driver
                Select Case nameValue
                    Case "modules"
                        driverProperty.Attribute("value").Value = "somedriver1"
                    Case "Artifact"
                        driverProperty.Attribute("value").Value = "somedriver2"
                    Case "Comments"
                        driverProperty.Attribute("value").Value = "somedriver3"
                    ' 可扩展添加其他name值的处理逻辑
                End Select
            End If
        Next
        
        xdoc.Save("c:\temp\myFile.xml")
        Return True
    Catch ex As Exception
        ' 可在此添加异常日志记录逻辑
        Return False
    End Try
End Function

逻辑说明

  1. 节点提取:复用原有的XPath查询,获取所有需要修改的Source节点。
  2. 属性值获取:在每个Source节点下,筛选出name对应的property元素,提取其value属性。
  3. 目标节点定位:找到同节点下driver对应的property元素。
  4. 条件赋值:通过Select Case匹配name值,给driver的value设置对应内容。
  5. 保存结果:完成所有修改后,将XML保存到指定路径。

修改后的XML将完全符合预期结果,每个Source节点的driver值会根据同级name值自动更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 01:07:01