提问人:Manfred Singer 提问时间:6/15/2023 最后编辑:Manfred Singer 更新时间:6/15/2023 访问量:27
在属性存在时删除节点
Removing a node when an attribute exists
问:
我有以下xml文档。
<?xml version="1.0"?>
<report>
<feature tag="Config"/>
<feature tag="Runtime">
<feature tag="Output">
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="Html"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Html.Driver"/>
</feature>
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="Word"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Word.Driver"/>
</feature>
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="PDF"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Pdf.Driver"/>
</feature>
</feature>
<feature tag="Log"/>
</feature>
</report>
我想读取“Output”节点下方的所有三个子节点“Target”,以确定是否存在value=“Html”。
如果我找到值“Html”,则此子节点将未删除,我想删除“输出”下方的所有其他子节点,即“输出”下方仅存在“目标”的一个子节点。
我正在使用 XDocument 类,但我没有成功。
Dim doc As New XmlDocument
Dim nodes As XmlNodeList
doc.Load(Path)
nodes = doc.SelectNodes("/report/feature[@tag='Runtime']/feature[@tag='Output']/feature[@tag='Target']")
For Each node As XmlNode In nodes
If node.ChildNodes(0).Attributes("value").Value <> "Word" Then
node.RemoveAll()
End If
Next
doc.Save("c:\temp\test.xml")
“输出”下面的结果如下,因此“功能”标签仍然存在。
<feature tag="Output">
<feature>
</feature>
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="Word">
</property>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Word.Driver">
</property>
<property name="path" type="URI" editable="true" visible="true" required="true" value="">
</property>
</feature>
<feature>
</feature>
<feature>
</feature>
<feature>
</feature>
</feature>
答:
0赞
dbasnett
6/15/2023
#1
所以这是我使用的数据
Dim doc As XElement
' doc = XElement.Load("your path here")
'for test use a literal
doc = <report>
<feature tag="Config"/>
<feature tag="Runtime">
<feature tag="Output">
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="Html"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Html.Driver"/>
</feature>
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="Word"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Word.Driver"/>
</feature>
<feature tag="Target">
<property name="type" editable="false" visible="true" required="false" value="PDF"/>
<property name="driver" editable="false" visible="true" required="false" value="Telelogic.Pdf.Driver"/>
</feature>
</feature>
<feature tag="Log"/>
</feature>
</report>
和代码
Dim ie As IEnumerable(Of XElement)
ie = From el In doc...<feature>
Where el.@tag = "Output"
From sel In el...<property>
Where sel.@value = "Html"
Select sel.Parent
If ie.Count = 1 Then
Dim parent As XElement = ie(0).Parent
Dim hld As XElement = New XElement(ie(0))
ie(0).Parent.Nodes.Remove()
parent.Add(hld)
End If
结果
'doc after above
'<report>
' <feature tag="Config" />
' <feature tag="Runtime">
' <feature tag="Output">
' <feature tag="Target">
' <property name="type" editable="false" visible="true" required="false" value="Html" />
' <property name="driver" editable="false" visible="true" required="false" value="Telelogic.Html.Driver" />
' </feature>
' </feature>
' <feature tag="Log" />
' </feature>
'</report>
上一个:从 XML 读取数据的顺序
评论