简体   繁体   中英

Reading out a specific childNode from a generated XML - File using vb.net

I've been using my XMLReader for reading out XML-Files generated by MS InfoPath for quite a while. Now I face the problem that there are multiple nodes with the same generated name in different parent nodes and I need to separate them.

example:

If .NodeType = XmlNodeType.Element Then
     If .Name = "pc:DisplayName" Then
          projectteam &= vbTab
          pteamDataset = True
     End If 
End If

This is what I use so far to search if there are any elements in pc:DisplayName

So now I have this element in several groups. Which means if I still use this code all people from all groups would be saved into projectteam

Sadly it doesn't work with the full xPath:

If .Name = "my:projectteam1/pc:person/pc:DisplayName" Then
projectteam1 &= vbTab
If .Name = "my:projectteam2/pc:person/pc:DisplayName" Then
projectteam2 &= vbTab

Is there any other way to call for a specific childNode or do I really have to display the data recursivly?

XmlReader doesn't keep track of the path for each element, but you could track it yourself, perhaps something like:

Dim path = New Stack(Of String)()
Using r = New XmlTextReader(...)
    While r.Read()
        If r.NodeType = XmlNodeType.Element Then
            path.Push(r.Name)
            Dim fullPath = String.Join("/", path.Reverse())
            ' May need .EndsWith, since root element will be in path?
            If fullPath = "my:projectteam1/pc:person/pc:DisplayName" Then
                projectteam1 &= vbTab
            ElseIf fullPath = "my:projectteam2/pc:person/pc:DisplayName" Then
                projectteam2 &= vbTab
            End If
        ElseIf r.NodeType = XmlNodeType.EndElement Then
            path.Pop()
        End If
    End While
End Using

The path stack is used to track the path to the current element, and fullPath contains the XPath-like path to the current element that you can check against.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM