如何确定XmlNode是否具有特定属性?

MG.*_*MG. 4 xml vb.net

我想在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)


XML文件的示例

<city>
    <store test="test.doc">
        <books>
        "New Words" 
        </books>
    </store>
</city>
Run Code Online (Sandbox Code Playgroud)

Gav*_*ler 7

假设Location是您要检查的节点,以查看您的属性是否存在:

If Location.Attributes.ItemOf("test") Is Nothing Then
    'Attribute doesnt exist
Else
    'Attribute does exist
End If
Run Code Online (Sandbox Code Playgroud)


ybo*_*ybo 7

尝试类似的东西:

If Not doc.SelectSingleNode("//store[@test='test.doc']") Is Nothing Then
    Exit Sub
End If
Run Code Online (Sandbox Code Playgroud)