Hi
I have to read an xml and add the node elements into a hashtable with
nodename as key and nodetext as value. If the selected node has
childnodes, then value should go as an array.
for eg.,
<Root>
<mysection>
<logroot>e:\tem p</logroot>
<outputdir>
<dir1>e:\temp 1</dir1><dir2>e:\t emp2</dir2>
</outputdir>
</mysection>
<myanothersecti on></myanothersectio n>
</Root>
I would first search for "Root/mysection". Now since logroot doesn't
have further childnodes, I would like to add to the hashtable "logroot"
as key and "e:\temp" as value. The next node outputdir has 2 child nodes
dir1 and dir2. I would like to create a string array for the values of
dir1 and dir2 and add to the hashtable "outputdir" as key and the array
as value.
The following code is not working for me.
Dim strXml As String =
"<Root><mysecti on><logroot>e:\ temp</logroot><output dir><dir1>e:\te mp1</d
ir1><dir2>e:\te mp2</dir2></outputdir></mysection><myan othersection></mya
nothersection></Root>"
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml( strXml)
Dim xmlSection As XmlNode =
xmlDoc.SelectSi ngleNode("Root/mysection")
Dim htValues As New Hashtable
For Each node As XmlNode In xmlSection.Chil dNodes
If (node.HasChildN odes) Then
Dim str() As String
ReDim str(node.ChildN odes.Count - 1)
For i As Integer = 0 To node.ChildNodes .Count - 1
str(i) = node.ChildNodes (i).InnerText
Next
htValues.Add(no de.Name, str)
Else
htValues.Add(no de.Name, node.InnerText)
End If
Next
I always get node.HasChildNo des as true even though the node doesn't
have further child nodes. Because of this, I end up adding an array as
value for every node. If the values inside the node is empty then it is
working fine.
Any help is appreciated.
Regards
rjn
*** Sent via Developersdex http://www.developersdex.com ***