Results 1 to 2 of 2

Thread: Simple XML class Example

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Simple XML class Example

    Hi this is a small class I made over the last two days I needed something basic for an address book so I thought of using xml as I always used ini files. anyway this class is basic allows you to read and write class and example below Comments and suggestions welcome.

    Class code

    xmlclass.vb

    vbnet Code:
    1. Imports System.Xml
    2. Imports System.IO
    3. Imports System.Text
    4.  
    5. Public Class XmlClass
    6.  
    7.     Private m_Filename As String = vbNullString
    8.     Private m_Root As String = "Settings"
    9.  
    10.     Public Property Root As String
    11.         Get
    12.             Return m_Root
    13.         End Get
    14.         Set(ByVal value As String)
    15.             m_Root = value
    16.         End Set
    17.     End Property
    18.  
    19.     Public Property Filename As String
    20.         Get
    21.             Return m_Filename
    22.         End Get
    23.         Set(ByVal value As String)
    24.             m_Filename = value
    25.         End Set
    26.     End Property
    27.  
    28.     Private Sub NewXmlDocument()
    29.         Dim xmlTextWrite As New XmlTextWriter(Me.Filename, Encoding.UTF8)
    30.         'Write blank xml doucment.
    31.         With xmlTextWrite
    32.             .WriteStartDocument(True)
    33.             .WriteStartElement(Root)
    34.             .WriteEndElement()
    35.             .WriteEndDocument()
    36.             .Flush()
    37.             .Close()
    38.         End With
    39.     End Sub
    40.  
    41.     Public Sub WriteString(ByVal Selection As String, ByVal Name As String, ByVal Value As String)
    42.         Dim xmlDoc As New XmlDocument()
    43.  
    44.         'Check if xml filename is here.
    45.         If Not File.Exists(Me.Filename) Then
    46.             'Create new blank xml file.
    47.             NewXmlDocument()
    48.         End If
    49.  
    50.         'Load xml document.
    51.         xmlDoc.Load(Me.Filename)
    52.  
    53.         'Check for settings selection.
    54.         If xmlDoc.SelectSingleNode(Root & "/" & Selection) Is Nothing Then
    55.             'Create selection.
    56.             xmlDoc.DocumentElement.AppendChild(DirectCast(xmlDoc.CreateElement(Selection), XmlNode))
    57.         End If
    58.  
    59.         'Check for element node
    60.         Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode(Root & "/" & Selection & "/Element[@Name='" & Name & "']")
    61.  
    62.         If xmlNode Is Nothing Then
    63.             Dim element As XmlElement = xmlDoc.CreateElement("Element")
    64.             'Write new element values.
    65.             element.SetAttribute("Name", Name)
    66.             element.SetAttribute("Value", Value)
    67.             'Add new node.
    68.             xmlDoc.DocumentElement(Selection).AppendChild(DirectCast(element, XmlNode))
    69.         Else
    70.             'Update node values.
    71.             xmlNode.Attributes("Name").Value = Name
    72.             xmlNode.Attributes("Value").Value = Value
    73.         End If
    74.  
    75.         'Save xml data.
    76.         xmlDoc.Save(Me.Filename)
    77.         xmlDoc = Nothing
    78.  
    79.     End Sub
    80.  
    81.     Public Function ReadString(ByVal Selection As String, ByVal Name As String, Optional ByVal DefaultValue As String = vbNullString) As String
    82.         Dim xmlDoc As New XmlDocument()
    83.  
    84.         If Not File.Exists(Me.Filename) Then
    85.             Return DefaultValue
    86.         Else
    87.             'Load xml document.
    88.             xmlDoc.Load(Filename)
    89.  
    90.             'Read node value.
    91.             Dim XmlNode = xmlDoc.SelectSingleNode(Root & "/" & Selection & "/Element[@Name='" & Name & "']")
    92.  
    93.  
    94.  
    95.             'Check if node is here.
    96.             If XmlNode Is Nothing Then
    97.                 Return DefaultValue
    98.             Else
    99.                 'Return xml node value
    100.                 Return (XmlNode.Attributes("Value").Value)
    101.                 'Clear up
    102.                 xmlDoc = Nothing
    103.             End If
    104.         End If
    105.     End Function
    106.  
    107.     Public Sub RemoveNode(ByVal Selection As String, ByVal Name As String)
    108.         Dim xmlDoc As New XmlDocument()
    109.         'Remove xml node
    110.         If File.Exists(Me.Filename) Then
    111.             'Load xml document.
    112.             xmlDoc.Load(Filename)
    113.  
    114.             'Read node value.
    115.             Dim XmlNode = xmlDoc.SelectSingleNode(Root & "/" & Selection & "/Element[@Name='" & Name & "']")
    116.  
    117.             'Check if node is here.
    118.             If Not XmlNode Is Nothing Then
    119.                 xmlDoc.SelectSingleNode(Root & "/" & Selection).RemoveChild(XmlNode)
    120.                 'Update xml document.
    121.                 xmlDoc.Save(Filename)
    122.             End If
    123.         End If
    124.  
    125.     End Sub
    126.  
    127. End Class

    Example

    vbnet Code:
    1. Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
    2.         Dim myXml As New XmlClass
    3.         Dim BookMark As String = vbNullString
    4.  
    5.         myXml.Filename = "C:\out\book.xml"
    6.         myXml.Root = "Main"
    7.         'Write some data lets use a bookmark example.
    8.  
    9.         myXml.WriteString("Bookmark", "Name", "Google")
    10.         myXml.WriteString("Bookmark", "Website", "http://www.google.com")
    11.         myXml.WriteString("Bookmark", "Info", "Google Home Page!")
    12.  
    13.         'Read and show the boomark we just added.
    14.         BookMark = myXml.ReadString("Bookmark", "Website", "Error")
    15.         MessageBox.Show("BookMark: " & BookMark, "Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
    16.  
    17.         'Remove bookmark.
    18.         myXml.RemoveNode("Bookmark", "Website")
    19.  
    20.         'Just show agian make sure it gone.
    21.         BookMark = myXml.ReadString("Bookmark", "Website", "http://www.localhost")
    22.         MessageBox.Show("BookMark: " & BookMark, "Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
    23.     End Sub

  2. #2
    New Member
    Join Date
    Jun 2015
    Posts
    3

    Re: Simple XML class Example

    OMG Thanks!!
    Thanks!! Thanks!! Thanks!!
    Works Perfect!!

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