This tip explains how you can populate a datagrid with values of specific nodes of a XML file.
The XML file we are taking in this example contains month names. We have to retrieve month names corressponding to particular nodes. The month node itself is further divided into 3 parts. The first part contains the key name of the node, the second part is the data of the node and the third and last part contains the comment.
The XML node looks something like this:
<data name=“apr“ xml:space=“preserve“>
<value>April</value>
<comment>4th month</comment>
</data>
.Net code for retrieving xml node values and populate those into data grid…..
Dim iCounter As Integer = 0 Dim iCountKey As Integer = 0 Dim iCountVal As Integer = 0 Dim iCountCom As Integer = 0 Dim arrKey(iCountKey) As String Dim arrVal(iCountVal) As String Dim arrCom(iCountCom) As String Dim sXmlTempVal As String = Nothing 'set path for application files Dim strPath As String = "C:\Program Files\TestFiles" Dim strXml As String = "test.resx" Dim objReader As XmlReader 'set path for reading xml files objReader = New XmlTextReader(strPath & "\" & strXml) 'xml reader type variable 'clear array values Array.Clear(arrKey, 0, iCountKey) Array.Clear(arrVal, 0, iCountVal) Array.Clear(arrCom, 0, iCountCom) 'collect specific values from xml files While objReader.Read() If objReader.XmlSpace = XmlSpace.Preserve Then If objReader.Name <> "" Then sXmlTempVal = objReader.Name 'store temp value for node name End If If objReader.Name = "data" Then 'check node name is data or not 'collect data node values If objReader.MoveToNextAttribute Then If objReader.Value.Trim <> "" Then ReDim Preserve arrKey(iCountKey) 're diamention of array arrKey(iCountKey) = objReader.Value.Trim 'store data(key) node values into array iCountKey += 1 'increament of counter variable End If End If ElseIf objReader.Name = "" And objReader.Value.Trim() <> "" Then If sXmlTempVal = "value" Then 'check node name is value or not 'collect value node values ReDim Preserve arrVal(iCountVal) 're diamention of array arrVal(iCountVal) = objReader.Value.Trim 'store value node values into array iCountVal += 1 'increament of counter variable ElseIf sXmlTempVal = "comment" Then 'check node name is comment or not 'collect comment node values ReDim Preserve arrCom(iCountCom) 're diamention of array arrCom(iCountCom) = objReader.Value.Trim 'store comment node values into array iCountCom += 1 'increament of counter variable End If End If End If End While objReader.Close() 'close xml reader object 'reset grid DtGridResource.Visible = True 'populate grid with values of xml file For iCounter = 0 To iCountKey – 1 DtGridResource.Rows.Add() 'with key values DtGridResource.Rows(iCounter).Cells(0).Value = arrKey(iCounter) 'with value values DtGridResource.Rows(iCounter).Cells(1).Value = arrVal(iCounter) 'with comment values DtGridResource.Rows(iCounter).Cells(2).Value = arrCom(iCounter) Next
Result of this code is…………..
|
Thanks to elaborate your coding. Thanks monalisha i was looking for this.