我想在sub中放置一个if条件,告诉它在属性TEST ="test.doc"的xml节点STORE不存在时运行.任何建议都会很棒.我是vb的新手.
Sub InsertNode(ByVal doc As XmlDocument)
Dim City As XmlNode = doc.DocumentElement
Dim Location As XmlElement = doc.CreateElement("store")
Location.SetAttribute("test", "test.doc")
Dim books As XmlElement = doc.CreateElement("books")
books.InnerXml = "New Words"
Location.AppendChild(books)
City.AppendChild(store)
End Sub 'InsertNode
Run Code Online (Sandbox Code Playgroud)
<city>
<store test="test.doc">
<books>
"New Words"
</books>
</store>
</city>
Run Code Online (Sandbox Code Playgroud)
假设Location是您要检查的节点,以查看您的属性是否存在:
If Location.Attributes.ItemOf("test") Is Nothing Then
'Attribute doesnt exist
Else
'Attribute does exist
End If
Run Code Online (Sandbox Code Playgroud)
尝试类似的东西:
If Not doc.SelectSingleNode("//store[@test='test.doc']") Is Nothing Then
Exit Sub
End If
Run Code Online (Sandbox Code Playgroud)