英文:
Removing a node when an attribute exists
问题
你想要处理XML文档,只保留那些在"Output"节点下的"Target"子节点中具有"value"属性值为"Html"的子节点,对吗?以下是已翻译的代码部分:
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.SelectSingleNode("property[@name='type']").Attributes("value").Value <> "Html" Then
node.RemoveAll()
End If
Next
doc.Save("c:\temp\test.xml")
这段代码将删除在"Output"节点下,"Target"子节点的"value"属性值不是"Html"的所有子节点。
英文:
I have the following xml document.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?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>
<!-- end snippet -->
I want to read all three subnodes "Target" below the "Output" node in order to find out if the value="Html" is present.
If I find the value "Html", this subnode will be unremoved, all others below "Output" I want to remove, that below "Output" only exists one subnode of "Target".
I am using the XDocument class, but I have no success.
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")
The result below "Output" is the following, so the "Feature" tags are still remaining.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案1
得分: 0
以下是您提供的代码的中文翻译:
这是我使用的数据
Dim doc As XElement
' doc = XElement.Load("your path here")
' 为了测试,使用文字
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
'<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>
请注意,这是您提供的代码的直译中文翻译,不包括任何额外信息。如果您需要进一步的解释或翻译其他部分,请随时提出。
英文:
So here is the data I used
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>
And the code
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
And the results
'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论