我有一个SVG文件,如果值不是使用VBA的整数,我的问题是删除文本元素。经过搜索后找到了这篇文章,但无法使其对我有用Remove a parent node and all children based on text value in child
<?xml version="1.0" ?>
<svg>
<image height="1980" width="1530" x="0" xlink:href="file.png" y="0"/>
<g stroke="#000000" stroke-width="1">
<text fill="#000000" font-family="Arial" font-size="15" x="645" y="532">A</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1391" y="603">2</text>
<text fill="#000000" font-family="Arial" font-size="15" x="261" y="689">A</text>
<text fill="#000000" font-family="Arial" font-size="15" x="1008" y="749">4</text>
<text fill="#000000" font-family="Arial" font-size="14" x="1009" y="768">eD</text>
<text fill="#000000" font-family="Arial" font-size="16" x="117" y="765">A</text>
<text fill="#000000" font-family="Arial" font-size="14" x="199" y="1115">Q</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1393" y="1113">3</text>
<text>DRA</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1131" y="1189">(?YPI PL?I CEG)</text>
<text fill="#000000" font-family="Arial" font-size="19" x="340" y="1310">B</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1223" y="1310">6</text>
<text fill="#000000" font-family="Arial" font-size="18" x="1222" y="1364">7</text>
<text fill="#000000" font-family="Arial" font-size="19" x="755" y="1484">(?YAL I6 PL)</text>
</g>
</svg>
代码
Public Sub xml()
Set XMLDoc = _
CreateObject("Microsoft.XMLDOM")
XMLDoc.Async = "False"
XMLDoc.Load ("C:\Users\1\g2.xml")
strVulid = "eD"
XPath = "svg/g[/text = '" & strVulid & "']"
For Each n In XMLDoc.SelectNodes(XPath)
n.ParentNode.RemoveChild (n)
Next
XMLDoc.Save "C:\Users\1\sample.xml"
MsgBox "done"
End Sub
答案 0 :(得分:0)
以下使用xml linq从XDocument中删除元素。代码会解析所有属性,以防万一您需要额外的数据
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:\temp\test.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()
For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i
End Sub
End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String
End Class