Hi,
how to store and retrieve a class in xml format? The class contains the following members (name,address,city,emailid).
regards,
senthil
Printable View
Hi,
how to store and retrieve a class in xml format? The class contains the following members (name,address,city,emailid).
regards,
senthil
this is a sub that I use to edit and append an xml file. You will obviously have to modify it to fit your needs but you get the idea
VB Code:
Public Sub EditXML(ByVal name As String, ByVal comment As String, ByVal dink As Boolean) Dim XmlDocument As New XmlDocument Dim XmlNodeL As XmlNodeList Dim XmlN As XmlNode Dim bFound As Boolean Dim id As Integer = -1 Dim XmlRoot As XmlNode 'Load the document XmlDocument.Load(Application.StartupPath & "\data.xml") 'Create the nodelist XmlNodeL = XmlDocument.DocumentElement.SelectNodes("/targets/target") 'Loop through the node list and search for the name If XmlNodeL.Count > 0 Then For i As Integer = 0 To XmlNodeL.Count - 1 XmlN = XmlNodeL(i) If XmlN.ChildNodes(0).InnerText = name Then 'Set found true and set the id of the node bFound = True id = i Exit For End If Next End If If bFound Then 'If the name was found then edit the information 'Set the node XmlN = XmlNodeL(id) 'Set the Name node (first child) to the name XmlN.ChildNodes(0).InnerText = name 'Set the comment node (second child) to the name XmlN.ChildNodes(1).InnerText = comment 'set the dink node (third child) value XmlN.ChildNodes(2).InnerText = dink.ToString Else 'Otherwise create a new node 'Set the root node XmlRoot = XmlDocument.DocumentElement.SelectSingleNode("/targets") 'Create the target node Dim tNode As XmlNode = XmlDocument.CreateNode(XmlNodeType.Element, "target", "") 'Create the Name node Dim nNode As XmlNode = XmlDocument.CreateNode(XmlNodeType.Element, "name", "") 'Set the text of the name node nNode.InnerText = name 'Add the name node as a child of the target node tNode.AppendChild(nNode) 'Create the comment node nNode = XmlDocument.CreateNode(XmlNodeType.Element, "comment", "") 'Set the text of the comment node nNode.InnerText = comment 'Add the comment node as a child of the target node tNode.AppendChild(nNode) 'Create the dink node nNode = XmlDocument.CreateNode(XmlNodeType.Element, "dink", "") 'Set the text of the dink node nNode.InnerText = dink.ToString 'Add the dink node as a child of the target node tNode.AppendChild(nNode) 'Append the root node with the complete target node XmlRoot.AppendChild(tNode) End If 'Save the document XmlDocument.Save(Application.StartupPath & "\data.xml") 'Clean up XmlDocument = Nothing End Sub
Serializing and Deserializing will do it automatically, its what it is there for...