xml selectnodes

  • Thread starter Thread starter e-mid
  • Start date Start date
E

e-mid

i want to get childs of specific xml node. normally i use

xmlNodeList fooList = myNode.SelectNodes("foo");

but in compactframework , there is no selectNodes() method or
selectSingleNode() method.

what else can i use instead?
 
e-mid said:
i want to get childs of specific xml node. normally i use

xmlNodeList fooList = myNode.SelectNodes("foo");

The luxury of the desktop! (to get child nodes, SelectNodes() is
overkill.)
but in compactframework , there is no selectNodes() method or
selectSingleNode() method.

what else can i use instead?

Try myNode.ChildNodes. It's an XmlNodeList that you can
iterate over, and check the LocalName property on each
XmlNode in ChildNodes to see if it's "foo".


Derek Harmon
 
thknz Derek.


Derek Harmon said:
The luxury of the desktop! (to get child nodes, SelectNodes() is
overkill.)


Try myNode.ChildNodes. It's an XmlNodeList that you can
iterate over, and check the LocalName property on each
XmlNode in ChildNodes to see if it's "foo".


Derek Harmon
 
Here are some replacements I wrote which provide a subset of the "real"
framework:

' This function replicates the SelectSingleNode MSXML function only
to the extent required by

' expressions such as /starting/next/next/element or
/starting/next/next/element/@attribute

Public Function SelectSingleNode(ByVal xmldoc As XmlDocument, ByVal
sInput As String) As String

Dim sSelect() As String, sXML As String, i As Integer, xNode As
XmlNode, tNode As XmlNode

' Break down the input string

sSelect = Split(sInput, "/")

If UBound(sSelect) < 1 Then Return ""

' Get the first node

xNode = xmldoc.GetElementsByTagName(sSelect(1)).Item(0)

' go to the next-to-last element

For i = 2 To UBound(sSelect) - 1

If xNode.HasChildNodes Then

For Each tNode In xNode.ChildNodes

If tNode.Name = sSelect(i) Then

xNode = tNode

Exit For

End If

Next

Else

Return ""

End If

Next

' Handle last node or attribute

If Mid(sSelect(i), 1, 1) = "@" Then

If xNode.Attributes(Mid(sSelect(i), 2)) Is Nothing Then

Return ""

Else

Return xNode.Attributes(Mid(sSelect(i), 2)).InnerText

End If

Else

For Each tNode In xNode.ChildNodes

If tNode.Name = sSelect(i) Then

xNode = tNode

Exit For

End If

Next

If xNode Is Nothing Then Return ""

Return xNode.InnerText

End If



End Function



' This function replicates the SelectNodes MSXML function only to
the extent required by

' expressions such as /starting/next/next/element or
/starting/next/next/element/*

Public Function SelectNodes(ByVal xmldoc As XmlDocument, ByVal
sInput As String) As XmlNode()

Dim sSelect() As String, sXML As String, i As Integer, xNode As
XmlNode, tNode As XmlNode

Dim xList() As XmlNode

' Break down the input string

sSelect = Split(sInput, "/")

If UBound(sSelect) < 1 Then Return Nothing

' Get the first node

xNode = xmldoc.GetElementsByTagName(sSelect(1)).Item(0)

' go to the next-to-last element

For i = 2 To UBound(sSelect) - 1

If xNode.HasChildNodes Then

For Each tNode In xNode.ChildNodes

If tNode.Name = sSelect(i) Then

xNode = tNode

Exit For

End If

Next

Else

Return Nothing

End If

Next

' Handle last node

If Not xNode.HasChildNodes Then Return Nothing

For Each tNode In xNode.ChildNodes

If tNode.Name = sSelect(i) Or sSelect(i) = "*" Then

' Re-size array

If xList Is Nothing Then

' First time through, create the array

ReDim xList(0)

xList(0) = Nothing

Else

' Increase size by 1

ReDim Preserve xList(xList.Length())

End If

xList(xList.Length - 1) = tNode

End If

Next

If xList(0) Is Nothing Then Return Nothing

Return xList

End Function
 
Back
Top