1

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?

GrindelOh
  • 448
  • 1
  • 10
  • 24
  • Did you take a look at [this method](http://stackoverflow.com/a/241291/4747123) to pull "the xpath" from any given node? seems like a perfect fit. – bri Jan 26 '16 at 15:32

1 Answers1

1

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.

Mark
  • 8,140
  • 1
  • 14
  • 29
  • This is a well elaborated solution - sadly it doesn't fix my xPath problem. It seems that the `NodeType.Element` can only search for one element and not for whole xPaths. Is this true or am I missing something? – GrindelOh Jan 27 '16 at 08:12
  • @GrindelOh `XmlReader` is forward-only and doesn't track state such as the path to the current element (it is designed for high performance, not convenience) - so the `path` and `fullPath` variables are used above to track that instead. If you want to find arbitrary elements, and the documents are not huge, I would recommend using [LINQ to XML](https://msdn.microsoft.com/en-us/library/bb387098.aspx) or `XmlDocument` and [XPath](https://msdn.microsoft.com/en-us/library/d271ytdx%28v=vs.110%29.aspx). – Mark Jan 27 '16 at 14:31
  • @GrindelOh One other thing, `fullPath` will have the complete path to the current element - you didn't show the full structure of your XML, so you will need to adjust those checks as appropriate. – Mark Jan 27 '16 at 14:38
  • I tried your solution with the full xPath too - still same problem. As time was running out I formed a solution with `XmlDocument.Load()` using `cases` to roll up the xPath recursively. – GrindelOh Jan 28 '16 at 10:02