GR3ee3N GR3ee3N - 1 month ago 11
Vb.net Question

Get multiple conditions out of a xml file in vb

This is an example of what i have:

<Person name="NAME">
<Conditions>
<Condition>1</Condition>
</Conditions>
</Person>
<Person name="NAME">
<Conditions>
<Condition>1</Condition>
<Condition>2</Condition>
</Conditions>
</Person>
<Person name="NAME">
<Conditions>
<Condition>1</Condition>
<Condition>2</Condition>
<Condition>3</Condition>
</Conditions>
</Person>


How can i achieve that if Condition 1 is existing, for example Checkbox1 gets checked and if Condition 1 and 2 are existing Checkbox1 and Checkbox2 gets checked and so on?

Answer

If you're going to use .net's built in system.xml, then your xml needs to have a root node or it will throw an exception when you try to load it into an xmldocument object, as it is improperly formatted. To run the xml you provided, I had to wrap the code in "People" tags...but here's an example of a simple loop that goes through each Person, gets their name, and loops through each condition that is present:

    Dim yourXmlString As String = "[all that xml you have up there]"

    Dim doc As XmlDocument = New XmlDocument
    doc.LoadXml("<People>" & yourXmlString & "</People>")
    Dim PeopleNodes As XmlNodeList = doc.GetElementsByTagName("Person")

    For Each PersonNode As XmlNode In PeopleNodes
        Dim PersonName As String = PersonNode.Attributes("name").Value
        For Each ConditionNode As XmlNode In PersonNode.SelectNodes("Conditions/Condition")
            Debug.Print(PersonName & "-" & ConditionNode.InnerXml)
            Select Case ConditionNode.InnerXml
                Case "1"
                    'check checkbox 1 for PersonName
                Case "2"
                    'check checkbox 2 for PersonName
                Case "3"
                    'check checkbox 3 for PersonName
            End Select
        Next
    Next

Don't know what your end goal is, but you can use the select-case to check the different boxes by the person's name with that code