mak101 mak101 - 5 months ago 12
Vb.net Question

How to search xml "attribute name" in wildcard?

I am new to xml parser in vb.net and need help from xml and vb.net experts :)

I need to find attribute name for given value but attribute start with desired text.

my xml string is something like this

<Level1>
<Level2>
<Level3 Group="FirstGroup1" Value="FALSE"/>
<Level3 Group="**FirstGroup2**" Value="TRUE"/>
<Level3 Group="FirstGroup3" Value="FALSE"/>
<Level3 Group="FirstGroup4" Value="FALSE"/>
<Level3 Group="FirstGroup5" Value="FALSE"/>
<Level3 Group="SecondGroup1" Value="FALSE"/>
<Level3 Group="SecondGroup2" Value="FALSE"/>
<Level3 Group="SecondGroup3" Value="FALSE"/>
<Level3 Group="SecondGroup4" Value="TRUE"/>
<Level3 Group="SecondGroup5" Value="FALSE"/>
<Level3 Group="SecondGroup6" Value="FALSE"/>
</Level2>
</Level1>


I want attribute name from FirstGroup* which has Value True. In this case I am expecting FirstGroup2.

I would appreciate any help.

Thanks.

Answer

Since you didn't provide any code I don't know what API you want to use to parse the XML, but using LINQ to XML and XML literals:

Dim xml =
<Level1>
    <Level2>
        <Level3 Group="FirstGroup1" Value="FALSE"/>
        <Level3 Group="FirstGroup2" Value="TRUE"/>
        <Level3 Group="FirstGroup3" Value="FALSE"/>
        <Level3 Group="FirstGroup4" Value="FALSE"/>
        <Level3 Group="FirstGroup5" Value="FALSE"/>
        <Level3 Group="SecondGroup1" Value="FALSE"/>
        <Level3 Group="SecondGroup2" Value="FALSE"/>
        <Level3 Group="SecondGroup3" Value="FALSE"/>
        <Level3 Group="SecondGroup4" Value="TRUE"/>
        <Level3 Group="SecondGroup5" Value="FALSE"/>
        <Level3 Group="SecondGroup6" Value="FALSE"/>
    </Level2>
</Level1>


Dim firstGroup = (
    From l3 In xml...<Level3>
    Where l3.@Group.StartsWith("FirstGroup") AndAlso
        l3.@Value = "TRUE"
    Select l3.@Group
).FirstOrDefault()
Console.WriteLine(firstGroup)

FirstGroup2