Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help to check xml element for several conditions in VB.NET code.
1. If the SecurityGroup element doesn’t exist: set to IsConfidential to ‘false’
2. If the SecurityGroup element exists: set to IsConfidential to ‘true’ when @Word starts with CONF
3. Otherwise, set IsConfidential to ‘false’

How do I do this?

I am having trouble with if statements. I am able to check if the element SecurityGroup does not exist and sets the IsConfidential to False.
I am not able to do numbers 2 and 3

Here is XML Element
XML
<SecurityGroup Word="CONFGUA">Conf-Guardianship</SecurityGroup>


What I have tried:

VB
Dim objInsertUpdateCase As MMGService.InsertUpdateCase = New MMGService.InsertUpdateCase

If aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/SecurityGroup") Is Nothing Then
   objInsertUpdateCase.IsConfidential = False
ElseIf Not aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/SecurityGroup/@Word").InnerText.StartsWith("CONF") Then 'CONF' Is Nothing Then
    objInsertUpdateCase.IsConfidential = True
Else
   objInsertUpdateCase.IsConfidential = False
End If
Posted
Updated 22-Jul-19 8:45am

1 solution

You can do it with a single XPath query:
VB.NET
objInsertUpdateCase.IsConfidential = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/SecurityGroup[starts-with(@Word, 'CONF')]") IsNot Nothing
 
Share this answer
 
Comments
Member 11403304 22-Jul-19 15:22pm    
So I can use your code in an if statement and then say else objInsertUpdateCase.IConfonfidentail = False
Richard Deeming 22-Jul-19 15:24pm    
You don't need to - it does it all in one line.

* If the SecurityGroup element doesn't exist, it sets IsConfidential to False;
* If the Word attribute doesn't exist, it sets IsConfidential to False;
* If the Word attribute doesn't start with "CONF", it sets IsConfidential to False;
* Otherwise, it sets IsConfidential to True;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900