Results 1 to 3 of 3

Thread: how to store and retrieve a class in xml format?

  1. #1

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    how to store and retrieve a class in xml format?

    Hi,
    how to store and retrieve a class in xml format? The class contains the following members (name,address,city,emailid).

    regards,
    senthil
    God has been pleased to place as a king or cobbler do the work sincerely

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: how to store and retrieve a class in xml format?

    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:
    1. Public Sub EditXML(ByVal name As String, ByVal comment As String, ByVal dink As Boolean)
    2.         Dim XmlDocument As New XmlDocument
    3.         Dim XmlNodeL As XmlNodeList
    4.         Dim XmlN As XmlNode
    5.         Dim bFound As Boolean
    6.         Dim id As Integer = -1
    7.         Dim XmlRoot As XmlNode
    8.  
    9.         'Load the document
    10.         XmlDocument.Load(Application.StartupPath & "\data.xml")
    11.         'Create the nodelist
    12.         XmlNodeL = XmlDocument.DocumentElement.SelectNodes("/targets/target")
    13.  
    14.         'Loop through the node list and search for the name
    15.         If XmlNodeL.Count > 0 Then
    16.             For i As Integer = 0 To XmlNodeL.Count - 1
    17.                 XmlN = XmlNodeL(i)
    18.                 If XmlN.ChildNodes(0).InnerText = name Then
    19.                     'Set found true and set the id of the node
    20.                     bFound = True
    21.                     id = i
    22.                     Exit For
    23.                 End If
    24.             Next
    25.         End If
    26.  
    27.  
    28.         If bFound Then 'If the name was found then edit the information
    29.             'Set the node
    30.             XmlN = XmlNodeL(id)
    31.             'Set the Name node (first child) to the name
    32.             XmlN.ChildNodes(0).InnerText = name
    33.             'Set the comment node (second child) to the name
    34.             XmlN.ChildNodes(1).InnerText = comment
    35.             'set the dink node (third child) value
    36.             XmlN.ChildNodes(2).InnerText = dink.ToString
    37.  
    38.         Else 'Otherwise create a new node
    39.             'Set the root node
    40.             XmlRoot = XmlDocument.DocumentElement.SelectSingleNode("/targets")
    41.             'Create the target node
    42.             Dim tNode As XmlNode = XmlDocument.CreateNode(XmlNodeType.Element, "target", "")
    43.             'Create the Name node
    44.             Dim nNode As XmlNode = XmlDocument.CreateNode(XmlNodeType.Element, "name", "")
    45.             'Set the text of the name node
    46.             nNode.InnerText = name
    47.             'Add the name node as a child of the target node
    48.             tNode.AppendChild(nNode)
    49.             'Create the comment node
    50.             nNode = XmlDocument.CreateNode(XmlNodeType.Element, "comment", "")
    51.             'Set the text of the comment node
    52.             nNode.InnerText = comment
    53.             'Add the comment node as a child of the target node
    54.             tNode.AppendChild(nNode)
    55.             'Create the dink node
    56.             nNode = XmlDocument.CreateNode(XmlNodeType.Element, "dink", "")
    57.             'Set the text of the dink node
    58.             nNode.InnerText = dink.ToString
    59.             'Add the dink node as a child of the target node
    60.             tNode.AppendChild(nNode)
    61.             'Append the root node with the complete target node
    62.             XmlRoot.AppendChild(tNode)
    63.         End If
    64.         'Save the document
    65.         XmlDocument.Save(Application.StartupPath & "\data.xml")
    66.         'Clean up
    67.         XmlDocument = Nothing
    68.  
    69.     End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: how to store and retrieve a class in xml format?

    Serializing and Deserializing will do it automatically, its what it is there for...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width