In this tip we will see how to save datagrid values to an XML file. The data is to be stored in 3 nodes – data name, value and comment . The following code snippet demonstrates how to save datagrid values to an XML file.
Dim objDoc As New XmlDocument() 'object for access xml document Dim newNodeData As XmlNode = Nothing Dim listNodedata As XmlNodeList 'local counter variable Dim iCounter As Integer = 0 Dim iIndex As Integer = 0 'local array variable Dim arrKey(iCount) As String Dim arrNewVal(iCount) As String Dim arrNewCom(iCount) As String 'Set path for application files Dim strPath As String = "C:\Program Files\TestFiles" Dim strXml As String = "test.resx" 'storing updated grid values For iCounter = 0 To DtGridResource.Rows.Count - 1 ReDim Preserve arrKey(iCounter) 're diamention of array arrKey(iCounter) = DtGridResource(0, iCounter).Value 'store for key part ReDim Preserve arrNewVal(iCounter) 're diamention of array arrNewVal(iCounter) = DtGridResource(1, iCounter).Value 'store for new value part ReDim Preserve arrNewCom(iCounter) 're diamention of array arrNewCom(iCounter) = DtGridResource(2, iCounter).Value 'store for new comment part Next 'set path for access xml file objDoc.Load(strPath & "\" & strXml) 'replace specific node according to grid value changes in value and comment listNodedata = objDoc.GetElementsByTagName("data") 'list of data node of xml file newNodeData = objDoc.GetElementsByTagName("data")(0) 'node of data node of xml file For Each newNodeData In listNodedata 'execute loop for particular node position of data node If newNodeData.OuterXml.ToString.Contains(arrKey(iIndex)) Then newNodeData.ChildNodes(1).InnerText = arrNewVal(iIndex) 'changing corresponding value node part according to grid cell newNodeData.ChildNodes(3).InnerText = arrNewCom(iIndex) 'changing corresponding comment node part according to grid cell iIndex += 1 'increament index End If Next 'save changes into xml file objDoc.Save(strPath & "\" & strXml)
The XML file generated.
<?xml version="1.0" encoding="utf-8" ?> - <root> - <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> - <xsd:element name="root" msdata:IsDataSet="true"> - <xsd:element name="metadata"> - <data name="apr" xml:space="preserve"> <value>April</value> <comment>4th month</comment> </data> - <data name="aug" xml:space="preserve"> <value>August</value> <comment>8th month </comment> </data> - <data name="dec" xml:space="preserve"> <value>December</value> <comment>12 th month </comment> </data> - <data name="feb" xml:space="preserve"> <value>February</value> <comment>2 nd month </comment> </data> - <data name="jan" xml:space="preserve"> <value>January</value> <comment>1st month</comment> </data> - <data name="jul" xml:space="preserve"> <value>July</value> <comment>7 th month </comment> </data> - <data name="jun" xml:space="preserve"> <value>June</value> <comment>6 th month </comment> </data> - <data name="mar" xml:space="preserve"> <value>March</value> <comment>3rd month</comment> </data> - <data name="may" xml:space="preserve"> <value>May</value> <comment>5 th month </comment> </data> - <data name="nov" xml:space="preserve"> <value>November</value> <comment>11 th month </comment> </data> - <data name="oct" xml:space="preserve"> <value>October</value> <comment>10 th month </comment> </data> - <data name="sept" xml:space="preserve"> <value>September</value> <comment>9 th month </comment> </data> </root>
I was looking for this. How to save values from data grid to an XML file? Thank you monalisha for your help