Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB 2005: Write, Read and Edit XML File

Posted on 2006-05-20
5
Medium Priority
?
3,051 Views
Last Modified: 2008-09-08
First off, I am using Visual Basic 2005. I do not know a lot about XML, but I understand it is the preferred method to store information instead of INI or Registry as was done in the past.

I am writing an application which I want to store a few items in an XML file.  At first I will only have one set of these elements for one application, but I may want to do more later, but let's start simple.

The information I need to capture is as follows:

AppName   <--- Applications Name
SrcDir        <--- Source Directory
IncSub       <--- Include Sub Directories
DestDir      <--- Destinataion Directory
FileName   <--- FileName

Again at first I will only be doing this for one application but that may grow.

How do write this XML File from a Form with each element as a user input?
How would I then recall this information in to variables to use in the application?
How do I edit/update the information?

All help would be greatly appreciated.

Thanks
0
Comment
Question by:sneeri_c
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16725650
Hi sneeri_c,

not very original but found a snippet here that gives you a class to work with for the same purpose
source: http://www.experts-exchange.com/Q_20964588.html

hope this helps a bit
bruintje
0
 

Author Comment

by:sneeri_c
ID: 16726184
I was looking for an example for my situation. I learn better from see what I am doing and figuring out how it works. Can anyone else assist?
0
 
LVL 44

Accepted Solution

by:
bruintje earned 2000 total points
ID: 16727032
ryancys posted 2 functions xml write and xml read that should be enough to get you going

i've rewritten the answer to your situation
---------

1. design your xml settings file like, you can change the name and path to file later

c:\setting.xml

<?xml version="1.0" encoding="utf-8"?>
<Details>
  <mySettings>
    <AppName value="Bob" />
    <SrcDir value="e:\" />
    <IncSub value="test" />
    <DestDir value="f:\" />
    <FileName value="blh.csv" />
  </mySettings>
</Details>

2. to read a value use this function

Private Function readXMLValue(ByVal filePath As String, ByVal key As String) As String
    Dim xd As New Xml.XmlDocument()

    'load the xml file
    xd.Load(filePath)

    'query for a value
    Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
                              "/Details/mySettings/" & key)

    'return the value or nothing if it doesn't exist
    If Not Node Is Nothing Then
        Return Node.Attributes.GetNamedItem("value").Value
    Else
        Return ""
    End If
End Function

this line does all the work looking for the Details/mysettings/keyname

    Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
                              "/Details/mySettings/" & key)

if it found the node then it returns the value

    Return Node.Attributes.GetNamedItem("value").Value

3. to write a value use this function

Private Function writeXMLValue(ByVal filePath As String, ByVal key As String, ByVal NewValue As String) As Boolean
    Try
        Dim xd As New Xml.XmlDocument()

        'load the xml file
        xd.Load(filePath)

        'save value
        Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
                                       "/Details/mySettings/" & key), Xml.XmlElement)
        If Not Node Is Nothing Then
            'key found, set the value

            Node.Attributes.GetNamedItem("value").Value = NewValue

            xd.Save(filePath)
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Return False
    End Try
End Function

this line does all the work looking for the Details/mysettings/keyname

        Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
                                       "/Details/mySettings/" & key), Xml.XmlElement)

if it found the node then it write the value

        Node.Attributes.GetNamedItem("value").Value = NewValue

4. you'll use it like

in the Load settings button  event

  txtAppName.text = readXMLValue("c:\setting.xml", "AppName")
  txtSrcDir.text = readXMLValue("c:\setting.xml", "SrcDir")
  txtIncSub.text = readXMLValue("c:\setting.xml", "IncSub")
  txtDestDir.text = readXMLValue("c:\setting.xml", "DestDir")
  txtFileName.text = readXMLValue("c:\setting.xml", "FileName")

in the Save new settings button eventhandler

  writeXMLValue("c:\setting.xml", "AppName", txtAppName.text)
  writeXMLValue("c:\setting.xml", "SrcDir", txtSrcDir.text)
  writeXMLValue("c:\setting.xml", "IncSub", txtIncSub.text)
  writeXMLValue("c:\setting.xml", "DestDir", txtDestDir.text)
  writeXMLValue("c:\setting.xml", "FileName", txtFileName.text)

---------

hope this clarifies me posting the link
0
 

Author Comment

by:sneeri_c
ID: 16729398
Thanks so much for your help and patience.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16729414
glad i could help and thanks for the grade

if it wasn't for questions like yours i wouldn't be learning anything new either :-)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question