ari
ari

Reputation: 13

How to use if condition for xml node in vb.net

These are the sample of my xml file

<ROOT>
    <FILE1>
        <ENGLISH_LANGUAGE>
            <ANOTHER_TAG1></ANOTHER_TAG1>
        </ENGLISH_LANGUAGE>
        <MANDARIN_LANGUAGE>
            <ANOTHER_TAG2></ANOTHER_TAG2>
        </MANDARIN_LANGUAGE>
    </FILE1>
</ROOT>

Let's say I want to get to node. Can I do this by inserting if condition to detect xml tag node? Is that even possible..

What I have tried:

Dim root As String = file.DocumentElement.Name.ToString
Dim node As XmlNodeList = file.SelectNodes(root & "//FILE1")
xmlfile.GetElementsByTagName("ENGLISH_LANGUAGE")
//Logic goes here

What I did was to get "ENGLISH_LANGUAGE" tag, without using if condition. Is this way correct?

Edited: I would like to add some pseudocode sample as below,

if xml node <ENGLISH_LANGUAGE> exists
 Do something within this xml tag
if xml node <MANDARIN_LANGUAGE> exists
 Do something within this xml tag

Upvotes: 0

Views: 418

Answers (1)

Anu6is
Anu6is

Reputation: 2658

Dim root As String = file.DocumentElement.Name.ToString
Dim node As XmlNodeList = file.SelectNodes(root & "//FILE1")

'GetElementsByTagName returns a collection of matching nodes 
'or an empty collection if there are no matches
Dim list As XmlNodeList = xmlfile.GetElementsByTagName("ENGLISH_LANGUAGE")

'Your logic can then be based on the fact that the collection is populated
If list.Count() > 0 Then
    'DoSomething()
End If

'Or you can perform actions on the elements within the collection
For each xmlNode As XmlNode In list
    'DoSomething()
End For

Upvotes: 1

Related Questions