I've been through post after post after post for the last few hours trying to figure this out... im new to the XML parsing stuff so I am starting to get frustrated. Here is a sample of the XMl im trying to parse:

<level id="1">
    <prop n="PROPERTY" v="VALUE"/>
</level>
<level id="2">
    <prop n="PROPERTY" v="VALUE"/>
</level>

I need to be able to decend in to id=1 and id=2 on demand and retrieve property name PROPERTY with its corresponding value VALUE. Can someone point me in the right direction?

Here is a sample of traversing a simple xml structure

    Dim myxml = <root>
                    <level id="1">
                        <prop n="PROPERTY" v="VALUE"/>
                    </level>
                    <level id="2">
                        <prop n="PROPERTY" v="VALUE"/>
                    </level>
                </root>

    For Each level In myxml.Elements
        Debug.WriteLine("id=" & level.@id.ToString)
        For Each prop In level.Elements
            Debug.WriteLine("n=" & prop.@n.ToString & "  v=" & prop.@v.ToString)
        Next
    Next

Thank you for the response! I will be sure to try it as soon as I get back home.

Works on the string but when I try:

        Dim myPath As String = myDoc + DefaultLVL

        Dim reader As XDocument = XDocument.Load(myPath)

        For Each level In reader.Elements
            Debug.WriteLine("ID=" & level.@id.ToString)

            For Each prop In level.Elements
                Debug.WriteLine("n=" & prop.@n.ToString & " v=" & prop.@v.ToString)
            Next
        Next

I get: A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

And no information to further explain which part of the code is doing this.

Okay, I found out what the problem is. The XML document that I am trying to read (and write to eventually) has multiple roots. I know this is not a proper XML document, but there is nothing I can do about the document itself as it is generated by a separate program. Here is the code I have so far:

        Dim document As XmlReader = New XmlTextReader("..\..\tester.xml")

        While document.Read()
            Dim type = document.NodeType
            If type = XmlNodeType.Element Then
                If (document.Name = "level") Then
                    Debug.WriteLine("ID=" & document.GetAttribute("id"))
                End If
                If (document.Name = "prop") Then
                    Debug.WriteLine("n=" & document.GetAttribute("n"))
                    Debug.WriteLine("v=" & document.GetAttribute("v"))
                End If
            End If
        End While

If I wrap my tester.xml file with <root></root> it works fine.. unfortunately, the real file im reading does not have a single root. Is there a way to append temporary <root></root> elements on the fly (temporary) so I can properly read this file? For example, I know this doesn't work but it is essentially what I need to do

            Dim document As XmlReader = New XmlTextReader("..\..\tester.xml")
            document = "<root>" & document & "</root>"

Please help!

Add the wrapper then save it to a temporary file and read it back into the xmldocument object.

Thank you for the help!!! I would have been lost!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.