walking XML tree-need sample code VB.NET

R

RickH

Anyone have any working sample code to walk a XML source for Visual Studio
Infopath sp1?
I can't seem to get the syntax straight; no sample code in SDK or anywhere I
can find.
Generic XML Xpath samples give me sysntax errors.
I need to traverse two repeating tables that were generated from a SQL source.
Any help appreciated.
 
R

RickH

This code works partially, but has extra elements and skips values....
Anything obvious?

Structure :
Manifest_Request
id
name
etc..
Line_Item
itemtype
GroupCode
WSIDNUmber
etc..
Line_Item
itemtype etc..
Manifest_Request etc



<InfoPathEventHandler(MatchPath:="SubmitChanges",
EventType:=InfoPathEventType.OnClick)> _
Public Sub SubmitChanges_OnClick(ByVal e As DocActionEvent)
Dim ManifestGroup As String
Dim thisNode, xmlNode, thisChild As IXMLDOMNode
Dim xmlNodeList As IXMLDOMNodeList
' Write your code here.

thisNode =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Manifest_Request")
xmlNodeList = thisNode.childNodes
'MsgBox(xmlNodeList.length) ' = 7 why? extra are of nodeName
"#text" not "Manifest_Request"
For Each xmlNode In xmlNodeList
If xmlNode.nodeName <> "#text" Then 'skip weird nodeNames
'MsgBox("test1: " + xmlNode.xml & " nodeName: " &
xmlNode.nodeName)
MsgBox("WSIDNumber: " &
xmlNode.selectSingleNode("@WSIDNumber").text) 'works fine
MsgBox("@ManifestGroup: " &
xmlNode.selectSingleNode("@ManifestGroup").text) 'dam, skips every other value

ManifestGroup =
xmlNode.selectSingleNode("@ManifestGroup").text 'dam, skips every other value


Select Case ManifestGroup
Case "A"
MsgBox("A Group selected")

Case "B"
MsgBox("B Group selected")
End Select

End If
Next


End Sub
 
S

Steve van Dongen [MSFT]

This code works partially, but has extra elements and skips values....
Anything obvious?

Structure :
Manifest_Request
id
name
etc..
Line_Item
itemtype
GroupCode
WSIDNUmber
etc..
Line_Item
itemtype etc..
Manifest_Request etc



<InfoPathEventHandler(MatchPath:="SubmitChanges",
EventType:=InfoPathEventType.OnClick)> _
Public Sub SubmitChanges_OnClick(ByVal e As DocActionEvent)
Dim ManifestGroup As String
Dim thisNode, xmlNode, thisChild As IXMLDOMNode
Dim xmlNodeList As IXMLDOMNodeList
' Write your code here.

thisNode =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Manifest_Request")
xmlNodeList = thisNode.childNodes
'MsgBox(xmlNodeList.length) ' = 7 why? extra are of nodeName
"#text" not "Manifest_Request"

InfoPath loads XML in preserve whitespace mode. The #text nodes
contain whitespace. Using thisNode.selectNodes("*") instead of
thisNode.childNode will return non-whitespace child nodes.
For Each xmlNode In xmlNodeList
If xmlNode.nodeName <> "#text" Then 'skip weird nodeNames
'MsgBox("test1: " + xmlNode.xml & " nodeName: " &
xmlNode.nodeName)
MsgBox("WSIDNumber: " &
xmlNode.selectSingleNode("@WSIDNumber").text) 'works fine

xmlNode.getAttribute("WSIDNumber") is simpler.
MsgBox("@ManifestGroup: " &
xmlNode.selectSingleNode("@ManifestGroup").text) 'dam, skips every other value

ManifestGroup =
xmlNode.selectSingleNode("@ManifestGroup").text 'dam, skips every other value

I can't comment on these because they don't appear in your example
structure.

Regards,
Steve
 
R

RickH

Ignore the skipped values, something in my form is causing repeating tables
not to have all the values the form shows...debugging that now

I am getting 4 extra nodes in my list, like 1st one and evry other after
that causing:
xmlNode.nodeName="#text" instead of "Offload_Item"...
I ignore them in my code, but I wonder if I am doing something wrong to get
these?
nodelist: #text, Offload_Item,#text, Offload_Item,#text, Offload_Item,#test
 
R

RickH

Steve van Dongen said:
InfoPath loads XML in preserve whitespace mode. The #text nodes
contain whitespace. Using thisNode.selectNodes("*") instead of
thisNode.childNode will return non-whitespace child nodes.


xmlNode.getAttribute("WSIDNumber") is simpler.


I can't comment on these because they don't appear in your example
structure.

Regards,
Steve
 
M

Matthew Blain \(Serriform\)

Those nodes are the text! In fact, if your text is something like "2 > 1",
you might have multiple nodes, text "2 ", entity ">", text " 1". (don't know
if you will, but it's possible).

Easy solution:
nodeList =
thisXDocument.DOM.selectNodes("/dfs:myFields/dfs:dataFields/d:Manifest_Reque
st/*")

Will only select elements, not text, entity, attribute nodes, etc.

You can also
selectNodes(/dfs:myFields/dfs:dataFields/d:Manifest_Request/@ManifestGroup)

--Matthew Blain
http://tips.serriform.com/



RickH said:
Ignore the skipped values, something in my form is causing repeating tables
not to have all the values the form shows...debugging that now

I am getting 4 extra nodes in my list, like 1st one and evry other after
that causing:
xmlNode.nodeName="#text" instead of "Offload_Item"...
I ignore them in my code, but I wonder if I am doing something wrong to get
these?
nodelist: #text, Offload_Item,#text, Offload_Item,#text,
Offload_Item,#test
 
R

RickH

Cool, many thnx. Learning as I go...

Matthew Blain (Serriform) said:
Those nodes are the text! In fact, if your text is something like "2 > 1",
you might have multiple nodes, text "2 ", entity ">", text " 1". (don't know
if you will, but it's possible).

Easy solution:
nodeList =
thisXDocument.DOM.selectNodes("/dfs:myFields/dfs:dataFields/d:Manifest_Reque
st/*")

Will only select elements, not text, entity, attribute nodes, etc.

You can also
selectNodes(/dfs:myFields/dfs:dataFields/d:Manifest_Request/@ManifestGroup)

--Matthew Blain
http://tips.serriform.com/




Offload_Item,#test
 
R

RickH

When I change d:Manifest_Request") to d:Manifest_Request/*"), the selectnodes
fails.
 
R

RickH

This code fails on the msgbox line--":
Dim xmlNodeA, xmlNodeB As IXMLDOMNode
Dim xmlNodeListA, xmlNodeListB As IXMLDOMNodeList
xmlNodeListA =
thisXDocument.DOM.selectNodes("/dfs:myFields/dfs:dataFields/d:Offload_Request/*")
For Each xmlNodeA In xmlNodeListA
xmlNodeListB = xmlNodeA.childNodes
MsgBox("PendingA: " & xmlNodeA.selectSingleNode("@Pending").text)
 
M

Matthew Blain \(Serriform\)

Have you looked at xmlNodeA.xml?

RickH said:
This code fails on the msgbox line--":
Dim xmlNodeA, xmlNodeB As IXMLDOMNode
Dim xmlNodeListA, xmlNodeListB As IXMLDOMNodeList
xmlNodeListA =
thisXDocument.DOM.selectNodes("/dfs:myFields/dfs:dataFields/d:Offload_Reques
t/*")
For Each xmlNodeA In xmlNodeListA
xmlNodeListB = xmlNodeA.childNodes
MsgBox("PendingA: " &
xmlNodeA.selectSingleNode("@Pending").text)
 
M

Matthew Blain \(Serriform\)

That surprises me... * is the standard way to get child nodes, I just ran a
simple test and it worked. Take a look at xmlNodeListA in the debugger and
see what's there.

The For Each syntax doesn't always work. Greg Collins posted another way to
do this today in the thread "Traversing xmlnode a 2nd time fails...".

--Matthew
 
R

RickH

changing
'xmlNodeListB = xmlNodeA.childNodes
to
xmlNodeListB = xmlNodeA.selectNodes("*")
fixed it.
Seems to work now.
Looks like there are many ways that are syntactically correct, but that
produce incorrect results. Makes life fun...
 
R

RickH

Here is some generic sample VB.NET code for walking tree, 'specially from SQL
source:
Dim xmlNodeA, xmlNodeB As IXMLDOMNode
Dim xmlNodeListA, xmlNodeListB As IXMLDOMNodeList

xmlNodeListA = thisXDocument.DOM.selectNodes("/dfs:myFields/dfs:dataFields/*")
xmlNodeA = Nothing

xmlNodeA = xmlNodeListA.nextNode()
While Not xmlNodeA Is Nothing
'do stuff
xmlNodeListB = thisXDocument.DOM.selectNodes("*")
xmlNodeB = Nothing

xmlNodeB = xmlNodeListB.nextNode()
While Not xmlNodeB Is Nothing
'Do stuff
End While
xmlNodeListB.reset 'walk listB , again, if needed
xmlNodeB = Nothing

xmlNodeB = xmlNodeListB.nextNode()
While Not xmlNodeB Is Nothing
'Do different stuff
End While
xmlNodeA = xmlNodeListA.nextNode()
End While

Don't know if its the best way, but it does work and fairly easy to
understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top